0

I get the following error when I want to clone a line :

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

But I don't want to edit respective row but want to add it !

 var parametersDisplay = db.utilisations_parametres_affichages.Where(o => o.utpar_clie_id == originalClientId).ToList();
 foreach (var parameterDisplay in parametersDisplay)
 {
       parameterDisplay.utpar_clie_id = copyClientId;
       parameterDisplay.utpar_user_update = "Backoffice";
       db.utilisations_parametres_affichages.Add(parameterDisplay); // <== Exception
       db.SaveChanges();
 }

I have tried with EntityState.Added but it doesn't work too.

The table had two primary key, I want edit one only.

Edit : It's a composite key, with 2 fields

xav-stargate
  • 677
  • 1
  • 5
  • 16
  • 1
    How can you have 2 Primary key for one table? or have composite key(combined 2 columns)? and would be nice if you share your table structure for better answer. – LifeOfPi Mar 01 '18 at 14:19
  • Seems, you don't have PK for your table. If not then create one and test your code again. Don't forgot to update your EDMX. – LifeOfPi Mar 01 '18 at 14:26
  • My edmx is up to date, and I have a composite primary key with 2 columns. – xav-stargate Mar 01 '18 at 14:39
  • Your code is taking the existing parametersDisplay and trying to edit it. You need to create a new parameterDisplay inside your loop. Then set the properties the way you want them. Then you will be able to add it. – Sean Lange Mar 01 '18 at 14:51
  • Can you share the table structure. Which column you have included to make PK. Whether those columns are nullable? what datatype you have. – LifeOfPi Mar 01 '18 at 15:29
  • Well, updating PK values are not best practice or not recommended. As error message says column is part of object and consider as an Identity. If you change the PK value then you are trying to change the identity of an object which will be not allowed. Better drop the row and re create. – LifeOfPi Mar 01 '18 at 15:56
  • *But I don't want to edit respective row but want to add it* So why do you update existing entities? You should add new entities. – Gert Arnold Mar 01 '18 at 16:12
  • @GertArnold that's what I do with "db.utilisations_parametres_affichages.Add" no ? – xav-stargate Mar 02 '18 at 08:53
  • No, you add an existing object that is edited. – Gert Arnold Mar 02 '18 at 08:54
  • @GertArnold So can you explain why with a table without a composite key it's work ? var terminals = db.terminaux.Where(o => o.utilisations_terminaux.Any(t => t.utite_clie_id == originalClientId)).ToList(); foreach (var terminal in terminals) { terminal.term_user_update = "Backoffice"; db.terminaux.Add(terminal); db.SaveChanges(); } – xav-stargate Mar 02 '18 at 09:00
  • In that scenario you wouldn't be able to edit the primary key either. Sure, EF marks the enities as `Added`, but it's always tricky to use existing objects to add new ones. For one, they could have nested entities that inadvertently get added too. Always create new objects if you want to add them.. – Gert Arnold Mar 02 '18 at 09:32

1 Answers1

0

Check here make sure you have defined a primary a key. If you havent then any non null fields will serve as a concatenated primary key.

tbean
  • 7
  • 2