简体   繁体   English

EF 4.3.1添加如何影响已经存在的相关实体

[英]EF 4.3.1 How adding affects related entities that exist already

Imagine this graph: 想象一下这张图:

Device 1..* OperatingEnvironment 1..* NetworkEndpoint 设备1 .. *操作环境1 .. *网络端点

If I construct a detached entity graph in-memory and the Device at the root of the graph is new while the OperatingEnvironment exists and the NetworkEndpoint exists but is assigned to a different OperatingEnvironment, will EF figure all this out? 如果我在内存中构造了一个分离的实体图,并且当OperatingEnvironment存在且NetworkEndpoint存在但被分配给了一个不同的OperatingEnvironment时,图的根目录处的设备是新的,EF是否会解决所有这些问题?

When I say 'figure this out', its simply a case of walking the graph and doing (IfExist ? MergeValues : AddNew) Because the entities all have foreign keys, relationships should effectively get reassigned automatically during the property merge. 当我说“图”时,这只是走图并执行操作的一种情况(IfExist?MergeValues:AddNew)因为实体都具有外键,所以关系应该在属性合并期间有效地自动重新分配。

Is this how EF works or will I have to walk my own graph and apply this logic? 这是EF的工作方式,还是我必须走自己的图并应用此逻辑? If I do this, then my in-memory graph will have to be weakly linked, ie avoid linking via navigation properties and instead use the foreign key values, otherwise the whole graph will be added as as soon as I add one entity since EF will crawl the navigation properties. 如果执行此操作,则必须将内存中的图进行弱链接,即避免通过导航属性进行链接,而应使用外键值,否则整个图将在我添加一个实体后立即添加,因为EF将抓取导航属性。

Thanks - hope that's clear as mud. 谢谢-希望一切如泥泞。

You must do it yourselves . 你必须自己做 EF has no internal logic to detect if the entity exists in the database or not. EF没有内部逻辑来检测实体是否存在于数据库中。

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

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