实体框架7 ASP.NET MVC6多个外键到同一个表 [英] Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table
问题描述
您好我有同样的问题,一个古老的职位,在这里,该解决方案提供有不为我的MVC 6 EF7工作简单
公共类比赛
{
[键]
公众诠释MatchId {搞定;组; } 公开日期时间休息日{搞定;组; }
公众持股量HomePoints {搞定;组; }
公众持股量GuestPoints {搞定;组; } 公众诠释HomeTeamId {搞定;组; }
公众诠释GuestTeamId {搞定;组; } [ForeignKey的(HomeTeamId)]
[InverseProperty(HomeMatches)]
公共虚拟团队HomeTeam {搞定;组; } [ForeignKey的(GuestTeamId)]
[InverseProperty(AwayMatches)]
公共虚拟团队GuestTeam {搞定;组; }}公共类团队
{
公众诠释TeamId {搞定;组; }
公共字符串名称{;组; } 公共虚拟的ICollection<匹配和GT; HomeMatches {搞定;组; }
公共虚拟的ICollection<匹配和GT; AwayMatches {搞定;组; }
}
这是我找到的最好方式,因为我可以添加新的迁移和一切正常,但是当我更新数据库我得到这样一个错误
引进国外KEY约束'FK_Match_Team_HomeTeamId对表'匹配'可能会导致循环或多重级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
无法创建约束或索引。见previous错误。
块引用>解决方案我preparing 答案,我可以建议你问题的两种解决方案。
问题在类的存在是因为两种特性
匹配
公众诠释HomeTeamId {搞定;组; }
公众诠释GuestTeamId {搞定;组; }和外键
HomeTeamId
和GuestTeamId
这将会产生。 EF7生成外键ON DELETE CASCADE
,它不能用于更作为一个外键。当前实现实体框架(RC1)都没有标注属性,它可以用来改变的行为。问题的第一个解决方案是使用可为空的属性,如
公众诠释? HomeTeamId {搞定;组; }
公众诠释? GuestTeamId {搞定;组; }或
公众诠释HomeTeamId {搞定;组; }
公众诠释? GuestTeamId {搞定;组; }最大的一个属性应该是非空。作为结果的问题就解决了,但有将有小的缺点,这可能是某些情形下并不重要。在为空的属性中的数据库表中的字段将在列定义没有
NOT NULL
属性。如果你需要同时按住
HomeTeamId
和GuestTeamId
非空的,那么你可以通过修改解决问题上下文类(从继承的DbContext),所在班匹配
和团队
中。您已经低于
一些上下文类定义的行公共类MyDBContext:的DbContext
{
DbSet<团队及GT;小组{搞定;组; }
DbSet<匹配和GT;匹配{搞定;组; }
}要解决,你可以在其中明确设置类添加保护
OnModelCreating
的说明问题。公共类MyDBContext:的DbContext
{
保护覆盖无效OnModelCreating(模型构建器模型构建器)
{
base.OnModelCreating(模型构建器); modelbuilder.Entity(typeof运算(比赛))
.HasOne(typeof运算(队),GuestTeam)
.WithMany()
.HasForeignKey(GuestTeamId)
.OnDelete(DeleteBehavior.Restrict); //没有ON DELETE modelbuilder.Entity(typeof运算(比赛))
.HasOne(typeof运算(队),HomeTeam)
.WithMany()
.HasForeignKey(GuestTeamId)
.OnDelete(DeleteBehavior.Cascade); //设置为ON DELETE CASCADE
} DbSet<团队及GT;小组{搞定;组; }
DbSet<匹配和GT;匹配{搞定;组; }
}您可以在两个外键使用的原因
DeleteBehavior.Restrict
(而不是DeleteBehavior.Cascade
的使用情况一)。此言的最后一个方法允许持有是很重要的两个HomeTeamId
和GuestTeamId
,就像在数据库中的相应字段,作为非空的。请参阅的其他信息的文档。
Hi I have the same problem that an old post that is here, the solution offer there doesn't work for me in MVC 6 with EF7 is simple
public class Match { [Key] public int MatchId { get; set; } public DateTime playday { get; set; } public float HomePoints { get; set; } public float GuestPoints { get; set; } public int HomeTeamId { get; set; } public int GuestTeamId { get; set; } [ForeignKey("HomeTeamId")] [InverseProperty("HomeMatches")] public virtual Team HomeTeam { get; set; } [ForeignKey("GuestTeamId")] [InverseProperty("AwayMatches")] public virtual Team GuestTeam { get; set; } } public class Team { public int TeamId { get; set; } public String name { get; set; } public virtual ICollection<Match> HomeMatches { get; set; } public virtual ICollection<Match> AwayMatches { get; set; } }
this is the best way that I found, because I can add a new migration and all is ok, but when I update the database I get an error like this
Introducing FOREIGN KEY constraint 'FK_Match_Team_HomeTeamId' on table 'Match' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
解决方案I analysed the problem in details during preparing the answer and I can suggest you two solutions of the problem.
The problem exist because of two properties in the class
Match
public int HomeTeamId { get; set; } public int GuestTeamId { get; set; }
and the foreign keys
HomeTeamId
andGuestTeamId
which will be generated. EF7 generate the foreign keys withON DELETE CASCADE
, which can't be used for more as one foreign key. The current implementation of Entity Framework (RC1) have no annotation attribute, which you can use to change the behavior.The first solution of the problem would be to use nullable properties like
public int? HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
or
public int HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
Maximum one property should be non nullable. As the result the problem will be solved, but one will have small disadvantage, which could be not important for some scenarios. The field in the database table for nullable property will have no
NOT NULL
property in the column definition.If you do need to hold both
HomeTeamId
andGuestTeamId
non-nullable then you can solve the problem by modifying the context class (inherited from DbContext), where the classesMatch
andTeam
be used.You have already some context class defined line below
public class MyDBContext : DbContext { DbSet<Team> Teams { get; set; } DbSet<Match> Matches { get; set; } }
To solve the describe problem you can add protected
OnModelCreating
in the class which explicitly setpublic class MyDBContext : DbContext { protected override void OnModelCreating(ModelBuilder modelbuilder) { base.OnModelCreating(modelbuilder); modelbuilder.Entity(typeof (Match)) .HasOne(typeof (Team), "GuestTeam") .WithMany() .HasForeignKey("GuestTeamId") .OnDelete(DeleteBehavior.Restrict); // no ON DELETE modelbuilder.Entity(typeof (Match)) .HasOne(typeof (Team), "HomeTeam") .WithMany() .HasForeignKey("GuestTeamId") .OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE } DbSet<Team> Teams { get; set; } DbSet<Match> Matches { get; set; } }
You can use of cause
DeleteBehavior.Restrict
on both foreign keys (instead of usage ofDeleteBehavior.Cascade
on one). It's important to remark that the last approach allows to hold bothHomeTeamId
andGuestTeamId
, like the corresponding fields in the database, as non-nullable.See the documentation for additional information.
这篇关于实体框架7 ASP.NET MVC6多个外键到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!