How to update aprimary key field using linq for SQL?

I have tbl_names with the following fields:

Id  int
Name nvarchar(10)
family nvarchar(20)


Id    Name    Family
 1     John     Smith

and suppose that Idand nameare primary keys together (composite primary key). and I want to update the field nameaccording to the value of the field Id.

DataclassesContext dac=new DataClassesContext();
var query=from record in Dac.tbl_name where record.id=1 select record;
query.name="Raymond";
Dac.Submitchanges();

but I ran into the following error:

Value of member 'co_Workshop' of an object of type 'Tbl_Workshop' changed.
A member defining the identity of the object cannot be changed.
Consider adding a new object with new identity and deleting the existing one instead.

Is this because of the field nameis the primary key? why can't i update the primary key field using linq?

+3
source share
4 answers

I'm not sure you should find a way around this. I can not imagine why it would be nice to change the value in the PC. The whole nature of a PC is that it is a stable string identifier.

PK "Id", , "", "". , "Id" , .

EDIT: , . , OP " ", , FK, PK, (to PK "Id", ). FK, , ( , RDBMS) :

  • FK
  • PK
  • PK "Id".
  • "" ""
  • FK, UNIQUE INDEX

Microsoft SQL Server, , FK, UNIQUE INDEX, , , LINQ PK "Id" .

+4

, .

+1

There was the same problem. For old reasons, I was not able to remove the column that I needed to update from the primary key part.

A simple solution was not to use Linq in this case, but to use T_SQL and make a simple update.

update tbl_name set name = 'Raymond' where id = 1
0
source

All Articles