Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 外键';是什么导致了例外_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 外键';是什么导致了例外

C# 外键';是什么导致了例外,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有一个名为Match的类,它表示两个团队之间的匹配,但是当我运行代码时,会出现一个异常 代码: } 例外情况: 在表“Matches”上引入外键约束“FK_dbo.Matches_dbo.Teams_TeamBId”可能会导致循环或多个级联路径。指定“在删除时不执行操作”或“在更新时不执行操作”,或修改其他外键约束。 无法创建约束。请参阅前面的错误 有没有办法解决这个问题,或者我可以对我的代码进行任何更改来解决这个问题?问题是EF默认使用ON DELETE级联创建FK约束。也就是说,当一个团队

我有一个名为Match的类,它表示两个团队之间的匹配,但是当我运行代码时,会出现一个异常

代码:

}

例外情况: 在表“Matches”上引入外键约束“FK_dbo.Matches_dbo.Teams_TeamBId”可能会导致循环或多个级联路径。指定“在删除时不执行操作”或“在更新时不执行操作”,或修改其他外键约束。 无法创建约束。请参阅前面的错误


有没有办法解决这个问题,或者我可以对我的代码进行任何更改来解决这个问题?

问题是EF默认使用ON DELETE级联创建FK约束。也就是说,当一个团队被删除时,它的所有比赛都会被自动删除。但是,由于一个匹配指向多个团队,这将创建一个循环删除循环,删除该匹配将需要删除另一个团队,这将需要删除其匹配,依此类推

要解决此问题,请使用fluent配置设置关联,以便手动关闭DELETE CASCADE。例如:

class MatchConfiguration : EntityTypeConfiguration<Match> {
    public MatchConfiguration() {
        this.HasRequired(m => m.TeamB)
            .WithMany(t => t.Matches) // if you don't have the property Team.Matches, you can pass no arguments here
            .HasForeignKey(t => t.TeamBId)
            .WillCascadeOnDelete(false);
    }
}

// in OnModelCreating in your db context:
builder.Configurations.Add(new MatchConfiguration);
类匹配配置:EntityTypeConfiguration{
公共匹配配置(){
此.HasRequired(m=>m.TeamB)
.WithMany(t=>t.Matches)//如果没有属性Team.Matches,则不能在此处传递任何参数
.HasForeignKey(t=>t.TeamBId)
.WillCascadeOnDelete(假);
}
}
//在数据库上下文中的OnModelCreating中:
builder.Configurations.Add(新的匹配配置);
也有一种方法可以只使用属性配置来指定它,但我不知道

class MatchConfiguration : EntityTypeConfiguration<Match> {
    public MatchConfiguration() {
        this.HasRequired(m => m.TeamB)
            .WithMany(t => t.Matches) // if you don't have the property Team.Matches, you can pass no arguments here
            .HasForeignKey(t => t.TeamBId)
            .WillCascadeOnDelete(false);
    }
}

// in OnModelCreating in your db context:
builder.Configurations.Add(new MatchConfiguration);