实体框架代码首先定义关系/键 [英] Entity Framework Code First - Defining Relationships/Keys

查看:110
本文介绍了实体框架代码首先定义关系/键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到这个错误:


在表发票上引入FOREIGN KEY约束SalesOrder_Invoices可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

无法创建约束。查看以前的错误。


我试图拥有以下关系/键:

   - > = 1到许多关系




  1. 客户 - >客户位置

  2. 客户位置 - > SalesOrder

  3. SalesOrder - >发票

  4. SalesRep - - > SalesOrder

  5. PaymentTerm - >客户

  6. PaymentTerm - > SalesOrder

  7. PaymentTerm - >发票

我正在按照以下标准定义它们:

 < ClassName>< PrimaryKeyID> 

示例:客户 ID 属性,所以在 CustomerLocation 中,我定义外键,如下所示:

 公共财产CustomerID AS Integer 

我所要做的只是定义外键正确吗?我还要为每个键定义导航属性?



而且,我不能在对象的相同主键上有多个外键?



更新



所以要定义一个关系,你是否使用 ClassName.PrimaryKeyProperty ?还是使用导航属性?或两者?困惑!!



更新2



所以要做一个关系工作,你有定义双方...我认为。

 公共类客户
公共属性ID AS整数
公共可覆盖的属性位置AS ICollection(OF CustomerLocation)

结束类

公共类CustomerLocation
公共属性ID AS整数
公共属性CustomerID AS Integer

结束类


解决方案

当您有多个级联删除路径时,由SQL Server引起的异常。如果您删除了PaymentTerm,它将触发所有三个关系的级联删除。在创建 SalesOrder 发票时,这将会炸毁。 EF默认情况下创建与 ON DELETE CASCADE 的所有一对多关系,您可以将您的具体关系重新映射为不使用它:

  modelBuilder.Entity< ...>()
.HasRequired(...)
.WithMany(...)
.HasForeignKey(...)
.WillCascadeOnDelete(false);

或者您可以通过删除约定将其关闭全局:

  modelBuilder.Conventions.Remove< OneToManyCascadeDeleteConvention>(); 

您可以通过编辑生成的 Up来绕过特定迁移的此错误()方法与一行如下:

  AddForeignKey(dbo.Payments, EventID,dbo.Events,EventID,cascadeDelete:true)

code> cascadeDelete:在违规关系上的值为false。


I am designing my database using code first and I need a little help I think.

I am getting this error:

Introducing FOREIGN KEY constraint 'SalesOrder_Invoices' on table 'Invoices' 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. See previous errors.

I am trying to have the following relationships/keys:

--> = 1 to Many Relationship

  1. Customer --> CustomerLocation
  2. CustomerLocation --> SalesOrder
  3. SalesOrder --> Invoice
  4. SalesRep --> SalesOrder
  5. PaymentTerm --> Customer
  6. PaymentTerm --> SalesOrder
  7. PaymentTerm --> Invoice

I am trying to define them by the standard of:

<ClassName><PrimaryKeyID>

Example: Customer has ID property, so in CustomerLocation i define the foreign key like so:

Public Property CustomerID AS Integer

All I have to do is define the foreign key correct? Do I also have to have navigation properties for each key I define?

And, can I not have multiple foreign keys on the same primary key of an object?

Updated

So to define a relationship, do you use the ClassName.PrimaryKeyProperty? or do you use navigation properties? Or both? Confused!!

Update 2

So to make a relationship work you have to define both sides... I think.

Public Class Customer
    Public Property ID AS Integer
    Public Overrideable Property Locations AS ICollection(OF CustomerLocation)

End Class

Public Class CustomerLocation
    Public Property ID AS Integer
    Public Property CustomerID AS Integer

End Class

解决方案

This is exception caused by SQL server when you have multiple paths of cascade deletes. If you delete your PaymentTerm it will trigger cascade delete on all three relations. This will blow up when creating either SalesOrder or Invoice. EF creates by default all one-to-many relations with ON DELETE CASCADE you can remap your specific relation to not use it by:

modelBuilder.Entity<...>()
            .HasRequired(...)
            .WithMany(...)
            .HasForeignKey(...)
            .WillCascadeOnDelete(false);

Or you can turn it off globaly by removing the convention:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();   

You can get around this error on a particular migration by editing the generated Up() method with a line something like this:

AddForeignKey("dbo.Payments", "EventID", "dbo.Events", "EventID", cascadeDelete: true)

and change that cascadeDelete: value to false on the offending relationship(s).

这篇关于实体框架代码首先定义关系/键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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