asp.net-mvc – 在实体框架代码中为同一表定义多个外键
发布时间:2020-05-23 12:24:19 所属栏目:asp.Net 来源:互联网
导读:我在我的MVC应用程序中有两个实体,我用Entity Framework 6 Code First方法填充数据库。学生实体有两个城市编号;其中一个为BirthCity,另一个为WorkingCity。当我如上定义外键时,迁移后在Student表中创建一个名为City_ID的额外列。认为有错误或如何定义这些F
|
我在我的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 – 按IN子句排序SQL Server结果
- iis-7 – ASP 3.0应用程序对象
- asp.net – 如何在更新面板刷新后运行一些javascript?
- ASP.Net MVC JQuery在IE8中未定义,但在Chrome中没问题
- asp.net-mvc – 从ASP.NET MVC中的部分视图中删除逻辑
- ASP.NET中的401.2的customerrors
- asp.net-mvc – 如何使更多MapHttpRoutes为MVC 4 Api
- ASP.NET Ajax客户端框架无法加载.将ScriptManager放在空白页
- 自定义ASP.NET容器控件
- asp.net – 很好的复杂linq到sql示例?
推荐文章
站长推荐
- rest – asp.net Web Api – 默认错误消息
- ASP.NET主页面和内容页面中的表单元素
- asp.net-mvc – .NET MVC / Entity Framework应用
- asp.net-mvc-3 – MVC-Mini-Profiler错误地显示重
- asp.net – 在哪里可以下载DLR的Managed JScript
- asp.net-mvc-3 – Telerik MVC网格,在运行时从集
- 经典ASP中的会话
- asp.net – 如何指定javascript运行时显示ModalP
- asp.net – 为什么IIS Express使用而不是?
- asp.net-mvc-4 – MVC4/DotNetOpenAuth中的自定义
热点阅读
