none
The property "UNIQUEID" is part of the object's key information and cannot be modified RRS feed

  • Question

  • Hi,

    Can someone explain me what goes wrong here ?

    I need to retrieve a list of records from my db, change some info and save them

    List<DB_PART> lstComponents = db.DB_PART.Where(a => a.PROJECTID.Equals(pl.PROJECTID) && a.PIPELINEID.Equals(pl.PIPELINEID)).ToList();

    I want to change a value in that list an re-insert it into the db, kind op copy with new PK

        lstComponents.ForEach(a =>
                {
                    DB_PART c = new DB_PART()
                    {
                        CHANGED = a.CHANGED,
                        CREATED = a.CREATED,
                        DESCRIPTION = a.DESCRIPTION,
                        DIAMETERSDN = a.DIAMETERSDN,
                        DIAMETERSINCH = a.DIAMETERSINCH,
                        GROUPID = a.GROUPID,
                        HEATNUMBER = a.HEATNUMBER,
                        INVENTAR = a.INVENTAR,
                        LENGTH = a.LENGTH,
                        LOTNUMBER = a.LOTNUMBER,
                        PARTID = a.PARTID,
                        PIPELINEID = newId,
                        PROJECTID = a.PROJECTID,
                        THICKNESSDN = a.THICKNESSDN,
                        THICKNESSINCH = a.THICKNESSINCH
                    };
    
                    db.Entry(c).State = EntityState.Added;
                    db.DB_PART.Add(c);
    
    
                });

    but I always get an error : The property "UNIQUEID" is part of the object's key information and cannot be modified

    Does someone see what goes wrong ? The edmx file was generated by Visual Studio and I alse have recreated it to be sure that is has the right db schema.

    Thanks,

    Serge


    Tuesday, November 17, 2015 9:28 AM

Answers

  • I want to change a value in that list an re-insert it into the db, kind op copy with new PK

    You can't do this. You can't get an existing object, update it and try to do some kind of an add/insert of the object back into the DB table.

    All you can do is get the object based on its primary, update values in the existing object and tell EF to update the existing record in the database based on new values that have been updated or changed in the object. Of course, the primary-key property in the object, you do not touch on a update of the existing record in the table.

    Tuesday, November 17, 2015 2:44 PM