【发布时间】:2019-09-22 11:09:10
【问题描述】:
我的代码正在将新行添加到我的站点配置文件表中,但是当有人尝试更新配置文件时,我不确定如何处理。我正在从控制器更新多个表。
我有以下代码。如果 ID 已经存在,我会检查客户表,如果是,则更改要修改的实体的状态。(我在网上找到了此代码)。我已经注释掉了下一行,因为它给了我一个错误。
此代码在保存更改时不会引发任何错误,但不会更新数据库。
var oldCustomer = _context.Customers.Find(objSv.CustomerServices.strUserID);
var oldCustomerServices = _context.CustomerServices;
if (oldCustomer == null) {
_context.Customers.Add(obj);
_context.CustomerServices.Add(objSv.CustomerServices);
}
else
{
_context.Entry(oldCustomer).State = EntityState.Modified;
// _context.Entry(oldCustomerServices).State = EntityState.Modified;
}
_context.SaveChanges();
我想用新对象更新数据库。这些是我的带有新数据的新对象
CustomerProfile obj = GetCustomerProfile();
ServiceProvider objSv = GetServiceProvider();`enter code here`
【问题讨论】:
标签: c# asp.net asp.net-mvc entity-framework model-view-controller