为什么“EntityKey 与 EntityKey 中对应的值不匹配”?
首先我想展示相应的代码片段。当涉及 objCtx.AttachTo() 时,它会抛出一个错误:
Error: "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."
// convert string fragIds to Guid fragIds
var fragIdsGuids = docGenResult.FragIds.Select(c => new Guid(c)).ToList();
//add each fragment to document))))
foreach (Guid fragIdsGuid in fragIdsGuids)
{
var fragment = new Fragment() { EntityKey = new EntityKey("DocTestObjectContext.Fragments", "ID", fragIdsGuid) };
objCtx.AttachTo("Fragments", fragment);
}
objCtx.SaveChanges();
我已经检查了所有内容,并且没有丢失任何主键。
然而,我需要一些话来解释为什么我认为我必须这样做。
我在 C# 环境中使用 EF4。 我在两个表之间有多对多关系,文档和片段(主键“ID”)(文档可以有许多片段,一个片段可以是许多文档的一部分) 实体模型对我来说效果很好。
然而,当我尝试向数据库添加新文档时,我手中已经有了相关片段的 ID。为了将新文档添加到数据库中,我必须调用每个 Fragmentobject 并将其添加到文档对象中的映射引用中。这是一个瓶颈,因为一个文档可能有超过 1000 个片段。结果是我每个文档需要 1 秒。不多,但我必须创建 3000 多个文档,保存这一秒会提高速度。
希望你知道这里出了什么问题。
谢谢。 托马斯
第一次编辑:
这是实际有效的解决方案。我想避免加载所有片段,而只是保存映射表中已有的片段 GUID。
// convert string fragIds to Guid fragIds
var fragIdsGuids = docGenResult.FragIds.Select(c => new Guid(c)).ToList();
// get responding entities from Fragment table
var fragmentList = objCtx.Fragments.Where(c => fragIdsGuids.Contains(c.ID)).ToList();
foreach (var fragment in fragmentList)
{
doc.Fragment.Add(fragment);
}
objCtx.SaveChanges();
第二次编辑:
我觉得我不太清楚我想要做什么。 但是,我想将片段表中的现有片段链接/引用到文档表中的对应文档。我想参考的文档是一份新文档。文档到片段表具有多对多的关系。这种关系在数据库中有一个链接表。在模型中,它被正确建模为多对多关系。没关系。
到目前为止,一切都很好。有效的是您在我的第一次编辑下看到的内容。我必须通过文档的 id 加载所有必要的片段
// get responding entities from Fragment table
var fragmentList = objCtx.Fragments.Where(c => fragIdsGuids.Contains(c.ID)).ToList();
,然后我才能将它们添加到我的文档实体中:
foreach (var fragment in fragmentList)
{
doc.Fragment.Add(fragment);
}
但是为什么我必须加载整个实体(片段)才能将其链接到新的实体文档。为什么不告诉 EntityStateManager “老兄,这里有一些 Fragment ID,链接它们!”?
此外,我尝试关注 Adrian 在评论中提到的 MSDN 文章。这对我来说不起作用。
First I'd like to show the corresponding code snippet. When it comes to objCtx.AttachTo() it throws me an error:
Error: "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."
// convert string fragIds to Guid fragIds
var fragIdsGuids = docGenResult.FragIds.Select(c => new Guid(c)).ToList();
//add each fragment to document))))
foreach (Guid fragIdsGuid in fragIdsGuids)
{
var fragment = new Fragment() { EntityKey = new EntityKey("DocTestObjectContext.Fragments", "ID", fragIdsGuid) };
objCtx.AttachTo("Fragments", fragment);
}
objCtx.SaveChanges();
I've checked everything and I'm not missing any primary key.
However I need some words to explain why I think I have to do it this way.
I'm using EF4 in a C# Environment.
I have a many to many relationship between two tables, Document and Fragments(Primary key "ID") (Documents can have many fragments and a fragment can be a part of many documents)
The Entity Model works fine for me.
However when I try to add a new document to the DB I already have the IDs of the related Fragments in my hand. For adding a new document to the DB I have to call each Fragmentobject and add it to the mapped reference in my document-object. This is a bottleneck because a document can have more than 1000 fragments. The Consequence is that I need 1sec per document. Not much, but I have to create more than 3000 documents and saving this second would result in more speed.
Hopefully you know what's wrong in here.
Thanks.
Thomas
1st edit:
here is the solution wich actually works. I would like to avoid to load all the fragments and instead just save the fragment GUID I already have in the mapping table.
// convert string fragIds to Guid fragIds
var fragIdsGuids = docGenResult.FragIds.Select(c => new Guid(c)).ToList();
// get responding entities from Fragment table
var fragmentList = objCtx.Fragments.Where(c => fragIdsGuids.Contains(c.ID)).ToList();
foreach (var fragment in fragmentList)
{
doc.Fragment.Add(fragment);
}
objCtx.SaveChanges();
2nd edit:
I have the feeling that it is not really clear what I try to do.
However I would like to link/reference existing fragments in a Fragment-table to a coressponding Document in a Document table. The Document I'd like to reference is a new one. The document to Fragment table has an many to many relationship. This relationship has a linking table on the database. In the model it is correctly modeled as a many to many relationship. That's fine.
So far so good. What works is what you can see under my first edit. I have to load all the necessary fragments for a document by their id
// get responding entities from Fragment table
var fragmentList = objCtx.Fragments.Where(c => fragIdsGuids.Contains(c.ID)).ToList();
After that I'm able to add them to my document entity:
foreach (var fragment in fragmentList)
{
doc.Fragment.Add(fragment);
}
But why the hell do I have to load the whole entity (fragments) only to link it to a new document. Why do not tell the EntityStateManager "Dude, here you have some Fragment IDs, link them!"?
Further I tried to follow the MSDN article mentioned by Adrian in the comments. This doesn't worked out for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试这个:
Dump 函数来自 LinqPad
I'll try this:
The Dump function is from LinqPad