相关疑难解决方法(0)

在Entity Framework Code First中为同一个表定义多个外键

我的MVC应用程序中有两个实体,我使用Entity Framework 6 Code First方法填充数据库.学生实体中有两个城市ID; 其中一个为BirthCity,另一个为WorkingCity.当我在上面定义外键时,在迁移后在Student表中创建一个名为City_ID的额外列.我有错误或如何定义这些FK?提前致谢.

学生:

public class Student
{
    public int ID { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; }

    public int BirthCityID { get; set; }

    public int LivingCityID { get; set; }


    [ForeignKey("BirthCityID")]
    public virtual City BirthCity { get; set; }

    [ForeignKey("LivingCityID")]
    public virtual City LivingCity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)


市:

public class City
{
    public int ID { get; set; }

    public string CityName …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc entity-framework foreign-keys foreign-key-relationship ef-code-first

36
推荐指数
2
解决办法
4万
查看次数