【发布时间】:2020-06-16 11:32:21
【问题描述】:
与here的上一个问题一样
我愿意将这些数据更新为:
employeeTable: FirstName, LastName
departmentTable: departmentName
workingshift: duration
但是,现在我尝试修改我的department_id 它出现错误说:
属性“department_id”是对象关键信息的一部分,不能修改。
这是我对加入数据的查询:
var result = (from e in DSE.employees
join d in DSE.departments on e.department_id equals d.department_id
join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
其次,这是我尝试修改department_id的代码
var result = (from e in DSE.employees
join d in DSE.departments on e.department_id equals d.department_id
join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
where d.department_id == 1
select d).FirstOrDefault();
if (result != null)
{
result.department_id = 2;
DSE.SaveChanges();
}
要求:
- 我的 ServicesEntities 是 DSE
- 我正在使用 实体框架
- 我的
department_id是连接对象,我的shift_id也是如此 - 我的 `department_id' 不是自动增量 ID
数据库属性:
Employee Table has employee_id, FirstName, LastName, Gender, Salary
Department Table has department_id, department_name
WorkingShift Table has shift_id, duration
问题:
假设我想按如下方式更新数据:
员工 名字 = 堆栈, 姓氏 = 溢出,
部门 部门 ID = 4, 部门名称 = 网络,
换档 Shift_id = 2,
我应该在上面的会话中执行什么编码?
【问题讨论】:
-
您的
select d应该是select e以获取员工并更新员工的部门 ID,而不是部门记录的 ID。 -
嗨@StevePy 太好了,我犯了这样的错误真是太棒了...所以我成功地更新了
employee table中的department_id。这是一个后续问题,我的 department 表包含 department_name 所以如果我想更新我的 department_name 那么我将使用select dam我说对了吗? -
@StevePy 我应该这样说,我想更改 employee FirstName 和 department_name 等数据跨越两个表,我应该在上面写什么查询?
-
嗨@Nasir 当我遵循史蒂夫给出的解决方案时,我没有遇到上述错误
标签: c# entity-framework linq stored-procedures inner-join