定义多个外键在实体框架code首先在同一个表 [英] Defining multiple Foreign Key for the Same table in Entity Framework Code First

查看:139
本文介绍了定义多个外键在实体框架code首先在同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体在我的MVC应用程序,我填充实体框架6 code第一种方法的数据库。有两个城市中的id学生实体;其中之一为BirthCity,其他为WorkingCity。当我定义了外键如上一个额外的列被创建在迁移后学生表命名City_ID。标识有错误或如何定义这些FKS?先谢谢了。

学生:

 公共类学生
{
    公众诠释ID {搞定;组; }    公共字符串名称{;组; }    公共字符串姓氏{搞定;组; }    公众诠释BirthCityID {搞定;组; }    公众诠释LivingCityID {搞定;组; }
    [ForeignKey的(BirthCityID)]
    公共虚拟城市BirthCity {搞定;组; }    [ForeignKey的(LivingCityID)]
    公共虚拟城市LivingCity {搞定;组; }
}


城市:

 公共类城市
{
    公众诠释ID {搞定;组; }    公共字符串CITYNAME {搞定;组; }
    公共虚拟的ICollection<的Student GT;学生{搞定;组; }
}


解决方案

要达到你需要你想要的东西提供一些aditional的配置。code首先约定可以识别双向关系,而不是在有
2 entities.You之间的多个双向关系可以添加配置(使用数据注释流利的API ),以present这
信息模型构建器。随着数据注释,你会用注解
名为<一个href=\"http://www.entityframeworktutorial.net/$c$c-first/inverseproperty-dataannotations-attribute-in-$c$c-first.aspx\"><$c$c>InverseProperty.随着流利的API,您使用的 / 相结合的方法来指定这些正确的目的关系。

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

 公共类学生
{
  公众诠释ID {搞定;组; }  公共字符串名称{;组; }  公共字符串姓氏{搞定;组; }  公众诠释BirthCityID {搞定;组; }  公众诠释LivingCityID {搞定;组; }
  [ForeignKey的(BirthCityID)]
  [InverseProperty(学生)]
  公共虚拟城市BirthCity {搞定;组; }  [ForeignKey的(LivingCityID)]
  公共虚拟城市LivingCity {搞定;组; }
}

此方式在指定明确地要涉及与学生导航属性的其他 BirthCity 导航属性关系结束。

使用的流畅API 可能是这样的:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
     modelBuilder.Entity&LT;的Student GT;()HasRequired(M = GT; m.BirthCity)。.WithMany(M = GT; m.Students).HasForeignKey(M = GT; m.BirthCityId);
     modelBuilder.Entity&LT;的Student GT;()HasRequired(M = GT; m.LivingCity)。.WithMany()HasForeignKey(M = GT; m.LivingCityId);
}

通过这最后的解决方案,你不需要使用任何attibute。

现在,@ChristPratt中的建议有学生城市类的集合,每个关系真的很有用。如果你这样做,然后使用配置数据注释可能是这样的:

 公共类学生
{
  公众诠释ID {搞定;组; }  公共字符串名称{;组; }  公共字符串姓氏{搞定;组; }  公众诠释BirthCityID {搞定;组; }  公众诠释LivingCityID {搞定;组; }
  [ForeignKey的(BirthCityID)]
  [InverseProperty(BirthCityStudents)]
  公共虚拟城市BirthCity {搞定;组; }  [ForeignKey的(LivingCityID)]
  [InverseProperty(LivingCityStudents)]
  公共虚拟城市LivingCity {搞定;组; }
}

或者使用流畅API 遵循同样的理念:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
     modelBuilder.Entity&LT;的Student GT;()HasRequired(M = GT; m.BirthCity)。.WithMany(M = GT; m.BirthCityStudents).HasForeignKey(M = GT; m.BirthCityId);
     modelBuilder.Entity&LT;的Student GT;()HasRequired(M = GT; m.LivingCity)。.WithMany(M = GT; m.LivingCityStudents).HasForeignKey(M = GT; m.LivingCityId);
}

I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance.

Student:

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; }
}


City:

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

    public string CityName { get; set; }


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

解决方案

To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.

Using Data Annotations could be like this:

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; }
}

This way you specifying explicitly that you want to relate the BirthCity navigation property with Students navigation property in the other end of the relationship.

Using Fluent Api could be like this:

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);
}

With this last solution you don't need to use any attibute.

Now, the suggestion of @ChristPratt in have a collection of Student in your City class for each relationship is really useful. If you do that, then the configurations using Data Annotations could be this way:

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; }
}

Or using Fluent Api following the same idea:

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);
}

这篇关于定义多个外键在实体框架code首先在同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆