Patrik Schweigl
Patrik Schweigl

Reputation: 93

The property 'ID" is part of the object's key on INSERT

We have to transfer data from one database to another. So I tried to write a program, which reads tables from the old database, create Entities and store them afterwards in the new database. At the beginning it worked very good. I tried to read only one table and transfer it to the new one. Now i receive the following error:

"The property 'Id' is part of the object's key information and cannot be modified.

No I dont get rid of that error. Even if I try to get back to the first implementation (which worked like a charm).Here I have the definition of the Table:

Table definition

And here the code:

    class MappingUtility
    {
        public static IEnumerable<Nation> MapNation(DataTable table, IModelFactoryService service)
        {
            IEnumerable<DataRow> rows = table.AsEnumerable();
            Nation nat = service.Create<Nation>();
            foreach(var nation in rows)
            {
                nat.Id = (System.Guid)nation.ItemArray[0];
                nat.HVCode = (string)nation.ItemArray[1];
                nat.Kurzbezeichung = (string)nation.ItemArray[2];
                nat.KFZ = (string)nation.ItemArray[3];
                nat.iso_a2 = (string)nation.ItemArray[4];
                nat.iso_a3 = (string)nation.ItemArray[5];
                nat.iso_n3 = (string)nation.ItemArray[6];
                nat.Vollbezeichung = (string)nation.ItemArray[7];
                nat.Updated = DateTime.Now;
                nat.Created = DateTime.Now;
                yield return nat;
            }
        }
    }

    using (var da = new SqlDataAdapter("SELECT * FROM NATION", "....."))
    {
        var table = new DataTable();
        da.Fill(table);
        using (var context = new DAtenbankContext())
        {
            int i = 0;
            foreach (var nation in MappingUtility.MapNation(table, ef6))
            {
                Debug.WriteLine(i++);
                if (context.Nation.Where(p => p.Id == nation.Id).FirstOrDefault() == null)
                {
                    try
                    {
                        context.Entry(nation).State = EntityState.Added;
                        context.SaveChanges();
                    }
                    catch(DbEntityValidationException ex)
                    {
                        Debug.WriteLine("");
                    }
                    catch (DbUpdateException ex)
                    {
                        Debug.WriteLine("There where some duplicate columns in the old table.");
                        Debug.WriteLine(ex.StackTrace);
                    }
                }
            }      
        }
    }

Note: The id is not autogenerated. If I try to create only one Nation at a time i can insert it. Even with this for loop I insert one nation, at the second iteration I get the error.

Upvotes: 0

Views: 127

Answers (1)

David
David

Reputation: 218847

I suspect that you're operating on the same instance of Nation with every iteration of the loop. It appears that you only ever create one instance and then modify it over time. Entity Framework is trying to track that instance, so modifying the key is confusing it.

Move the instantiation into the loop so that you're creating new instances:

IEnumerable<DataRow> rows = table.AsEnumerable();
foreach(var nation in rows)
{
    Nation nat = service.Create<Nation>();
    // ...
    yield return nat;
}

Upvotes: 1

Related Questions