Updating an entity object with relation to another table-object (NxN) (Inserting, deleting)

asp.net c# entity entity-framework entity-framework-6

Question

Using entity for a table with NxN-relation (to another table), I can´t update the first table object (with its second table inside it) because the program show an exception message related with the second table and it says there is a record with the same PK in the second table (but I am not updating the second table per se, I am updating the NxN-table which means the relations between the two tables).

This is the scenario:

TABLE1 - EntityObject1

Attribute1 int

Attribute2 string

Attribute3 string

Attribute4 List(EntityObject2)

TABLE2 - EntityObject2

Attribute1 int

Attribute2 string


So:

var previous = context.TABLE1.Include(path => path.TABLE2)
      .Where(p => p.Attribute1 == updateElement.Attribute1).FirstOrDefault();
previous.Attribute4 = updateElement.Attribute4;
context.SaveChanges();

Not working as I said.

Another approach tried was:

First - Remove previous from the context, and Save context.

Second - Add the updateElement and Save context (not taking care about the fact of the PK´s changes)

Is there an easy way to achieve this?

Completely lost... Thanks you mates.

1
0
9/5/2018 4:28:44 PM

Accepted Answer

Well, finally find it. I am posting the idea for helping others to achieve it, at least, using this way:

Note: We must to use transaction for handling the operation in one step.

For inserting

I generate the object - Table 1 as usual and for attribute 4, I generate a list of object - Table 2 (attaching first), then I asign list to attribute 4...

(using context = new Dbcontext())
{
  using (var transaction = context.Database.BeginTransaction(IsolationLevel.Serializable))
  {
    Table1 object1 = new Table1
    {
      Attribute1 = 1, //(autoincrement really)
      Attribute2 = "random value",
      Attribute3 = "random value",
    };

    foreach (Table2 obj2 in NewValues)
    {
      Table2 o = new Table2() {Attribute1 = obj2.int, Attribute2 = obj2.string};
      context.Table2.Attach(o);
      object1.Attribute4.Add(o);
    }
  }
}

For updating

I just erase, for inserting later.


Another way for the inserting:

Store List in a temp List Adding Object1 without Attribute4 to the context. For finally, add temp List to object1.Attribute4.

(using context = new Dbcontext())
{
  using (var transaction = context.Database.BeginTransaction(IsolationLevel.Serializable))
  {
    Table1 object1 = new Table1
    {
      Attribute1 = 1, //(autoincrement really)
      Attribute2 = "random value",
      Attribute3 = "random value",
    };

    List<Table2> tempTable2 = new List<Object2>();
    foreach (Table2 obj2 in NewValues)
    {
      Table2 o = new Table2() {Attribute1 = obj2.int, Attribute2 = obj2.string};
      tempTable2.Add(o);
    }

    context.Table1.Add(object1);
    context.SavesChanges();

    Table1 object11 = context.Table1.First(o => o.Attribute1 == object1.Attribute1);
    object11.Attribute4 = tempTable2
    context.SavesChanges();
  }
}
0
9/6/2018 1:04:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow