0

--here was wrong model without association manyTOmany between A-B. corrected is in EDIT2--

A exists in database, B exists in database. I need only enter new C element with some Properties1 and Properties2 (and update collections of C in existed A and B elements)

I tried many options, like for example this, but still somethings wrong (with ObjectOCntext and existed Key etc)

void SaveNewC(C newC)
{
    using (var context = new MyEntities(connectionString))
    {
        var dbA = context.A.Where(a => a.Id == newC.A.Id).SingleOrDefault();
        var dbB = context.B.Where(b => b.Id == newC.B.Id).SingleOrDefault();

        newC.A = dbA;
        newC.B = dbB;
        context.AddObject(newC);

        context.SaveChanges();
    }
}

EDIT

Exception that I get: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."

EDIT2 updated model

enter image description here

Saint
  • 5,397
  • 22
  • 63
  • 107
  • 1
    Can you post the entire exception you're getting? – Mark Oreta Sep 10 '12 at 15:41
  • 1
    Don't have `A` and `B` navigation collections refering to each other? You cannot have a many-to-many relationship without navigation properties. It's also important to know if and how those collections are populated in `newC.A` and `newC.B`. – Slauma Sep 11 '12 at 09:09
  • @Slauma You're right. Last time I improve model incorrect without navigtion properties. Now model is fully cmplete – Saint Sep 11 '12 at 09:14
  • Try to clear all references in `newC.A` and `newC.B`: `newC.A.C = null; newC.A.B.Clear();` and `newC.B.C = null; newC.B.A.Clear();` and check if it works then. – Slauma Sep 11 '12 at 10:37
  • @Slauma Yes, I tried it before, but still the same exception – Saint Sep 11 '12 at 11:16
  • There must be something more which is not visible in your example code. If all those references are cleared this exception is impossible. – Slauma Sep 11 '12 at 13:15
  • 1
    Your model seems really confusing. You have a Many to Many on A -> B which is being mapped, I assume with C. Why do you need to have the C entity at all? EF will automatically hide the junction table away and manage that. If you're planning on having custom logic on C then you should no longer have the Many to Many directly from A->B, but it should go A -> C -> B instead. Otherwise you've created another hidden away junction table (D maybe?) Is this what you intended to do? – Mark Oreta Sep 11 '12 at 13:21
  • @Slauma I flag this topic to moderator to remove. See more actual version: http://stackoverflow.com/questions/12372975/many-to-many-with-junction-table-in-entity-framework – Saint Sep 11 '12 at 15:26
  • What are you doing :( That's the same question as this one and still this mysterious exception... – Slauma Sep 11 '12 at 15:58

3 Answers3

0
context.Entry(dbA).State = EntityState.Unchanged;
context.Entry(dbB).State = EntityState.Unchanged;
context.AddObject(newC);
context.SaveChanges();
podiluska
  • 50,950
  • 7
  • 98
  • 104
  • There's no "State" property in EntityObject – Saint Sep 10 '12 at 14:47
  • If you mean `context.ObjectStateManager.ChangeObjectState(dbA, System.Data.EntityState.Unchanged);` I tried it, but I get InvalidOperationException: "`An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.`" - but C object is new one – Saint Sep 10 '12 at 15:00
0

Apparently your newC has already populated navigation properties A and B with the correct Ids. Then you can just attach the entities refered by the navigation properties to the context:

void SaveNewC(C newC)
{
    using (var context = new MyEntities(connectionString))
    {
        context.A.Attach(newC.A);
        context.B.Attach(newC.B);

        context.C.AddObject(newC);

        context.SaveChanges();
    }
}
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • While `context.A.Attach(newC.A);` I get exception: `"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."` – Saint Sep 11 '12 at 08:03
  • 1
    @Saint: Is the code in your question really complete? The first line above happens in a new context, so this context is empty and nothing can't be attached to it before this line. The only reason I could imagine is that `newC.A` has references to other objects and some of these objects in the graph are of the same type and have the same key but are different objects. Does `newC.A` have references to other entities? – Slauma Sep 11 '12 at 08:40
  • Yes, A and B are in relationship many-to-many. I forgot to add this association at first time. See my EDIT2 – Saint Sep 11 '12 at 08:49
0

Do you need to have the junction table mapped? If all your wanting is a Many to Many from A->B you could do it in a simpler way.

If you created the C table as a true junction - and have a FK to A and B set to it's PK in sql like this:

enter image description here

Then when you create your edmx from the model it will create this:

enter image description here

Now, in your code if you wanted to add a relationship, you would simply add it to your collection, and EF will automatically create the relationship in your C table for you:

        var A = new A();
        var b = new B();
        var b2 = new B();

        A.B.Add(b);
        A.B.Add(b2);
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • No, I need this junction table – Saint Sep 11 '12 at 15:20
  • See more actual version of this topic, this one will be removed: http://stackoverflow.com/questions/12372975/many-to-many-with-junction-table-in-entity-framework – Saint Sep 11 '12 at 15:28