Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy "EntityKey does not match the corresponding value in the EntityKey"?
    primarykey
    data
    text
    <p>First I'd like to show the corresponding code snippet. When it comes to objCtx.AttachTo() it throws me an error: </p> <pre><code>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 =&gt; 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(); </code></pre> <p>I've checked everything and I'm not missing any primary key. </p> <p>However I need some words to explain why I think I have to do it this way.</p> <p>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.</p> <p>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.</p> <p>Hopefully you know what's wrong in here.</p> <p>Thanks. Thomas</p> <p><strong>1st edit:</strong></p> <p>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.</p> <pre><code> // convert string fragIds to Guid fragIds var fragIdsGuids = docGenResult.FragIds.Select(c =&gt; new Guid(c)).ToList(); // get responding entities from Fragment table var fragmentList = objCtx.Fragments.Where(c =&gt; fragIdsGuids.Contains(c.ID)).ToList(); foreach (var fragment in fragmentList) { doc.Fragment.Add(fragment); } objCtx.SaveChanges(); </code></pre> <p><strong>2nd edit:</strong></p> <p>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.</p> <p>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</p> <pre><code>// get responding entities from Fragment table var fragmentList = objCtx.Fragments.Where(c =&gt; fragIdsGuids.Contains(c.ID)).ToList(); </code></pre> <p>After that I'm able to add them to my document entity:</p> <pre><code>foreach (var fragment in fragmentList) { doc.Fragment.Add(fragment); } </code></pre> <p>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!"?</p> <p>Further I tried to follow the MSDN article mentioned by Adrian in the comments. This doesn't worked out for me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload