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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 为什么@Using Html.BeginForm中的普通标记需
- asp.net-mvc-3 – 数据注释MVC3必需属性
- asp.net-mvc – ASP.MVC 2.0当整数值为零时,如何显示整数的
- asp.net – 如何从TableAdapter中检索存储过程返回值
- asp.net-mvc – 在ASP.NET MVC Preview 4中使用路由引擎进行
- ASP.NET Core文件上传与下载实例(多种上传方式)
- asp.net – 如何在WebPage中显示嵌入的Excel文件?
- .net-4.0 – 为什么我在一个新的MVC4项目中收到编译错误?
- asp.net-mvc – 如何在Asp.Net MVC循环中呈现纯HTML链接?
- asp.net – 向GridView Row添加ID