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

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

Question

I can't update the first table object (with its second table within it) while using entity for a table with NxN-relation (to another table), as the programme displays an exception message related to the second table and claims that 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).

Here is the situation:

EntityObject1 in TABLE1

the range Attribute1 int

string from zzz-24 zzz

string Attribute3

The Attribute4 List (EntityObject2)

EntityObject2 in TABLE2

The int Attribute1

string Attribute2


So:

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

Not operating as I described.

An additional strategy 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 a simple method to do this?

lost in the fog... Thank you, friends.

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

Accepted Answer

I did, however, locate it. I'm putting it out there in the hopes that it may, at the at least, inspire others to do the same:

Note: In order to handle the operation in one step, we must utilise transactions.

To be inserted

As per normal, I create an object in Table 1 and, for property 4, I create a list of objects in Table 2 (attaching first), after which I assign the 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);
    }
  }
}

As an update

I just delete in order to insert afterwards.


Another technique for inserting:

Keep List in a temporary List. adding Object1 to the context without Attribute4. Add temp List to object1 to complete. 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