简体   繁体   English

从EntityKey构造EntityObject而不在Entity Framework中进行数据库调用

[英]Constructing EntityObject from EntityKey without a database call in Entity Framework

Assuming I have a row in the database with a specific id which I know (in my sample below - a symbol with Id = 5), can I create an EntityObject which can be attached to another entity without the need to load it from the database (same scenario is applicable to "DeleteObject" as well, if you think about it...)? 假设我在数据库中有一行我知道的特定id(在我的下面的示例中 - Id = 5的符号),我可以创建一个EntityObject,它可以附加到另一个实体而无需从数据库加载它(同样的情况也适用于“DeleteObject”,如果你考虑一下......)?

I've tried the following: 我尝试过以下方法:

UserSpread spread = UserSpread.CreateUserSpread(5, 500000, 1.1m, 1.5m);

EntityKey symbolKey = new EntityKey("Entities.SymbolSet", "Id", 5);

Symbol symbol = new Symbol();
symbol.EntityKey = symbolKey;

// dealingEntities.Attach(symbol);

spread.Symbol = symbol;
Entities.AddToSpreadSet(spread);

I get the following exception on the "AddToSpreadSet()" method: 我在“AddToSpreadSet()”方法上得到以下异常:

"The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key." “该对象无法添加到ObjectStateManager,因为它已经有一个EntityKey。使用ObjectContext.Attach附加一个具有现有键的对象。”

If I try to attach the symbol before assigning it to the spread (commented line), I get the following on the "SaveChanges()" method: 如果我在将符号分配给spread(注释行)之前尝试附加符号,我在“SaveChanges()”方法上得到以下内容:

"The object cannot be attached because the value of a property that is a part of the EntityKey does not match the corresponding value in the EntityKey." “无法附加对象,因为作为EntityKey一部分的属性的值与EntityKey中的对应值不匹配。”

Any ideas? 有任何想法吗?
Thanks, 谢谢,
Nir 尼尔

The general approach I use is to just create a stub entity (by setting just the PK) and attach it to the context. 我使用的一般方法是创建一个存根实体(通过设置PK)并将其附加到上下文。

So here is an example: 所以这是一个例子:

Category existing = new Category {ID = 7};
ctx.AttachTo("Categories", existing); // Notice no use of EntityKey anywhere


// build a new product
...
product.Category = existing;
ctx.AddToProducts(product);
ctx.SaveChanges();

This works well for building relationships, but there are some caveats when deleting. 这适用于构建关系,但删除时有一些警告。 So should definitely check out my EF tips series for more information. 因此,请务必查看我的EF提示系列以获取更多信息。 And in particular this one: 特别是这一个:

Tip 9 - How to delete an object without retrieving it 技巧9 - 如何在不检索对象的情况下删除对象

Hope this helps 希望这可以帮助

Alex 亚历克斯

It is possible to do what you're asking. 你可以做你想要的事情。 You'll have to do something like the following: 您必须执行以下操作:

dealingEntities.symbolReference.EntityKey = new EntityKey("Entities.SymbolSet", "Id", 5); dealingEntities.symbolReference.EntityKey = new EntityKey(“Entities.SymbolSet”,“Id”,5);

Does that make sense? 那有意义吗? I may have your entity names mixed up so ask me if you need further information. 我可能会将您的实体名称混淆,因此请询问您是否需要进一步的信息。

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

相关问题 使用不带EntityObject的Entity Framework修改字段 - Modify field with Entity Framework without EntityObject 实体框架生成的类型不继承自EntityObject - Entity Framework generated types not inheriting from EntityObject 将Entity Framework 6与EntityKey一起使用 - Using Entity Framework 6 with EntityKey 实体框架(实体从EntityObject到DTO,然后又到EntityObject)是否可以在此时轻松地在db中更新它? - Entity Framework ( Entity travelled from EntityObject to DTO and then again to EntityObject ) is it possible to easily update it in db at this point? 没有PK或EntityKey的Entity Framework中的一对一关系 - One-to-one relationship in Entity Framework without PK or EntityKey 阻止实体框架序列化EntityKey,EntitySetName等 - Preventing Entity Framework from serializing EntityKey, EntitySetName, etc 实体框架 - 使用GUID作为EntityKey的SaveChanges - Entity Framework - SaveChanges with GUID as EntityKey 防止EntityObject更改为添加状态(实体框架) - Keep EntityObject from changing into Added state (Entity Framework) 实体框架,更改EntityKey会将Entity保留为“ UnChanged” - Entity Framework, changing EntityKey leaves Entity as “UnChanged” 实体框架EntityKey /外键问题 - Entity Framework EntityKey / Foreign Key problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM