加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在实体框架代码中为同一表定义多个外键

发布时间:2020-12-15 18:43:22 所属栏目:asp.Net 来源:网络整理
导读:我在我的MVC应用程序中有两个实体,我用Entity Framework 6 Code First方法填充数据库。学生实体有两个城市编号;其中一个为BirthCity,另一个为WorkingCity。当我如上定义外键时,迁移后在Student表中创建一个名为City_ID的额外列。认为有错误或如何定义这些
我在我的MVC应用程序中有两个实体,我用Entity Framework 6 Code First方法填充数据库。学生实体有两个城市编号;其中一个为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; }
}

市:

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

    public string CityName { get; set; }


    public virtual ICollection<Student> Students { get; set; }
}

解决方法

要实现你想要的,你需要提供一些有用的配置.Code第一个约定可以识别双向关系,但不是当有
两个实体之间的多重双向关系。您可以添加配置(使用数据注释或Fluent API)来呈现此信息
信息给模型构建器。使用数据注释,您将使用注释
称为 InverseProperty.使用Fluent API,您将使用Has / With方法的组合来指定这些关系的正确结尾。

使用数据注释可能是这样的:

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")]
  [InverseProperty("Students")]
  public virtual City BirthCity { get; set; }

  [ForeignKey("LivingCityID")]
  public virtual City LivingCity { get; set; }
}

通过这种方式,您明确指定要将BirthCity导航属性与“关系”另一端的“学生”导航属性相关联。

使用Fluent Api可能是这样的:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
                                 .WithMany(m => m.Students).HasForeignKey(m=>m.BirthCityId);
     modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
                                 .WithMany().HasForeignKey(m=>m.LivingCityId);
}

有了这个最后的解决方案,你不需要使用任何attibute。

现在,@ChristPratt的建议在每个关系的City类中都有一个学生的收藏是非常有用的。如果这样做,那么使用数据注释的配置可能是这样的:

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")]
  [InverseProperty("BirthCityStudents")]
  public virtual City BirthCity { get; set; }

  [ForeignKey("LivingCityID")]
  [InverseProperty("LivingCityStudents")]
  public virtual City LivingCity { get; set; }
}

或使用Fluent Api遵循相同的想法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
               .WithMany(m => m.BirthCityStudents).HasForeignKey(m=>m.BirthCityId);
     modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
               .WithMany(m => m.LivingCityStudents).HasForeignKey(m=>m.LivingCityId);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读