简体   繁体   English

EF6代码优先:具有多个关系的实体

[英]EF6 Code First : Entity with multiple relationships

I am creating a new database using EF code-first which contain the following classes: 我正在使用EF代码优先创建一个新数据库,其中包含以下类:

Address, Contact, Account, Customer (a sub-class of Account), and SalesOrder. 

The Address class is the one giving me problems at the moment, it can have no foreign key because it can be linked to any of the other five classes (with more to come), and each of the other classes can have one or more navigation properties pointing back to it. Address类是目前给我带来麻烦的一个类,它没有外键,因为它可以链接到其他五个类中的任何一个(还有更多类),并且每个其他类可以具有一个或多个导航指向它的属性。

The navigation properties should look as follows: 导航属性应如下所示:

Contact.AddressId?
Contact.Address

Account.AddressId?
Account.Address

Customer.DeliveryAddresses

SalesOrder.InvoiceAddressId
SalesOrder.InvoiceAddress

SalesOrder.DeliveryAddressId?
SalesOrder.DeliveryAddress

It should be possible for these classes to share the same Address record, eg an Account has an Address, this can also be linked to a SalesOrder, a different Address, linked to the Customer, could be linked to another SalesOrder. 这些类应该可以共享相同的地址记录,例如,一个帐户具有一个地址,也可以将其链接到一个SalesOrder,将另一个链接到Customer的地址,也可以链接到另一个SalesOrder。 All Addresses linked to Accounts and Customers should be unique, but other classes should be able to share links to these Addresses. 链接到“帐户和客户”的所有地址都应该是唯一的,但是其他类别应该能够共享指向这些地址的链接。

I have tried setting it up with all the possible fluent configurations I can think of, with my DbContext having a DbSet property and without (ultimately I don't think it should have it's own DbSet property, as the Addresses should only be accessible from the various root objects, but if that's the only way to get it to work I'm happy to manage the inserts/deletes myself). 我尝试使用我能想到的所有可能的流畅配置进行设置,而我的DbContext具有DbSet属性,而没有(最终,我不认为它应该具有自己的DbSet属性,因为只能从各种根对象,但是如果这是使其工作的唯一途径,我很乐意自己管理插入/删除操作)。

I tried making all the navigation properties nullable (ideally SalesOrder.InvoiceAddressId should not be nullable), and also had to remove the Customer.DeliveryAddresses Many-to-Many mapping at one point because that was confusing the issue. 我尝试将所有导航属性设置为可为空(理想情况下,SalesOrder.InvoiceAddressId不应为可为空),并且还不得不一次删除Customer.DeliveryAddresses多对多映射,因为这使问题变得令人困惑。

I get various errors depending on how I have it set up, either Multiplicity conflicts due to non-nullable fields, or Cascade on Delete errors when I have no DbSet property and I try and let EF handle the inserts and deletes. 根据我的设置方式,我会遇到各种错误,或者由于不可为空的字段导致的多重性冲突,或者当我没有DbSet属性并且尝试让EF处理插入和删除操作时,在删除错误上发生级联。

I also end up with unwanted null rows when I do have a DbSet property set. 当我确实设置了DbSet属性时,我最终还会得到不需要的空行。 eg: 例如:

add three Address records to the DbSet (Address(1), Address(2), Address(3),
add two Accounts to the DbSet (Account(1) & Account(2)),
add multiple SalesOrders,
set Account(1).AddressId = 1
set Account(2).AddressId = 2, 
set SalesOrder(n).InvoiceAddressId = 1,
set SalesOrder(n).DeliveryAddressId = 3

This will correctly create the Address records, but the related keys will only be set correctly if the various Id foreign-key properties are used, rather than the navigation property, and even if the Id properties are used the foreign keys all look correct, but orphaned records for each SalesOrder (or two per order if both navigation properties are used) end up in my Address table with all their fields bar Id set to NULL. 这将正确创建地址记录,但是只有在使用了各种ID外键属性(而不是导航属性)的情况下,相关键才会正确设置,即使使用Id属性,外键也都看起来正确,但是每个SalesOrder的孤立记录(如果同时使用两个导航属性,则每个订单两个)在我的地址表中结束,其所有字段栏ID都设置为NULL。

The only thing I can think of that I haven't tried would be to create multiple sub-classes of Address and use each one with it's related class (eg SalesOrderDeliveryAddress), but that doesn't seem ideal. 我唯一没有想到的就是创建Address的多个子类,并将每个子类与它的相关类一起使用(例如SalesOrderDeliveryAddress),但这似乎并不理想。 I'd rather not do that unless I have to. 除非必须,否则我不愿意这样做。

Is what I'm looking for possible to set up in EF, or is there some other way to go about doing it? 我正在寻找可以在EF中进行设置的东西,还是有其他方法可以做到这一点?

Thanks, David 谢谢大卫

There are several issues making this confusing. 有几个问题使这个令人困惑。 To start with I would switch off the default cascade on delete to get rid of multiple cascade paths and come back to that later. 首先,我将在delete上关闭默认的层叠,以摆脱多个层叠路径,稍后再返回。

Then read about adding disconnected trees, foreign keys and navigation properties here: http://msdn.microsoft.com/en-us/magazine/dn166926.aspx 然后在此处阅读有关添加断开的树,外键和导航属性的信息: http : //msdn.microsoft.com/zh-cn/magazine/dn166926.aspx

Then I would set up the entities you way you want them and repost a more specific issue. 然后,我将按照您想要的方式设置实体,然后重新发布更具体的问题。 (You have tried lots of stuff so it's hard to work out what happens when here) (您已经尝试了很多东西,所以很难弄清楚这里发生了什么)

Once you've got adding and updating working you can come back and work out where you can put in cascade delete and where it needs to be manual 完成添加和更新工作后,您可以返回并确定可以级联删除的位置以及需要手动进行的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM