我有tbl_names与以下字段:如何用linq更新一个关键字段到SQL?
Id int
Name nvarchar(10)
family nvarchar(20)
Id Name Family
1 John Smith
和假设Id
name
和是主键合(化合物主键)。 ,我想根据Id
字段的值更新name
字段。
DataclassesContext dac=new DataClassesContext();
var query=from record in Dac.tbl_name where record.id=1 select record;
query.name="Raymond";
Dac.Submitchanges();
,但我碰到下面的错误:
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.
是不是因为name
字段是主键?为什么我不能使用linq更新主键字段?
我想不出有一次我更新了主键。不是说你没有理由...我从来没有。有趣的是,它不会让你。这可能是因为它是唯一可以链接到数据库的标识符?所以你必须用更新的密钥复制它,添加它,然后删除旧密钥。 – Mikecito 2011-05-09 03:53:46
为什么你会有'{id,name}'的联合身份?为什么不保持'id'独特? – 2011-05-09 03:54:49
我正在处理旧数据库,无法更改其结构。是的,你是对的没有理由,但我需要它 – 2011-05-09 03:57:39