外键错误 [英] Foreign Keys errors

查看:109
本文介绍了外键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

使用CTP5,我有一个模型,其中我有一个Relationship类,其中两个属性类型为Contact,名为Source和Target。

Using CTP5, i have a model where i have a Relationship class, with two properties of type Contact named Source and Target.

            modelBuilder.Entity<Relationship>().HasRequired(r => r.Source);
            modelBuilder.Entity<Relationship>().HasRequired(r => r.Target);

生成表时,我收到以下错误: 

When generating the tables, i get the following error : 

{"在表'关系'上引入FOREIGN KEY约束'Relationship_Target'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。\\\\ nn不能创建约束。请参阅
之前的错误。"}

{"Introducing FOREIGN KEY constraint 'Relationship_Target' on table 'Relationship' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."}

这不合法吗?

推荐答案

SQL Server不支持从一个表级联到另一个表的多个FK约束。 Code First会自动为所需关系引入级联删除,因此您只需将其中一个关闭即可。

SQL Server does not support having multiple FK constraints that cascade from one table to another. Code First automatically introduces a cascade delete for required relationships so you just need to turn it off on one of them.

modelBuilder
  .Entity<Relationship>()
  .HasRequired(r => r.Source)
  .WithMany()
  .WillCascadeOnDelete(false);

 

~Rowan


这篇关于外键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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