C# 使用automapper进行Linq更新? [HttpPost] 公共操作结果_EditCustomer(CustomServiceWModel CustomServiceWModel) { if(ModelState.IsValid) { 尝试 { Customers=entity.Customers.FirstOrDefault(x=>x.sno==CustomerViewModel.sno); customer=AutoMapper.Mapper.Map(CustomServiceWModel); entity.SaveChanges(); 返回内容(“Müteri düzenleme işlemi başarılı。”,“text/html”); } 捕获(例外e) { AddModelError(“,“Müteri güncelleme hatası”); } } //更新客户失败! CustomerViewModel.Cities=实体.Cities; CustomServiceWModel.PowerSuppliers=实体.PowerSuppliers; CustomerViewModel.Sectors=实体.Sectors; 返回PartialView(CustomerViewModel); }
我调试了代码,然后在运行时customer正在更新(automapper正在工作,我可以看到更改),但是C# 使用automapper进行Linq更新? [HttpPost] 公共操作结果_EditCustomer(CustomServiceWModel CustomServiceWModel) { if(ModelState.IsValid) { 尝试 { Customers=entity.Customers.FirstOrDefault(x=>x.sno==CustomerViewModel.sno); customer=AutoMapper.Mapper.Map(CustomServiceWModel); entity.SaveChanges(); 返回内容(“Müteri düzenleme işlemi başarılı。”,“text/html”); } 捕获(例外e) { AddModelError(“,“Müteri güncelleme hatası”); } } //更新客户失败! CustomerViewModel.Cities=实体.Cities; CustomServiceWModel.PowerSuppliers=实体.PowerSuppliers; CustomerViewModel.Sectors=实体.Sectors; 返回PartialView(CustomerViewModel); },c#,asp.net-mvc,linq,automapper,updates,C#,Asp.net Mvc,Linq,Automapper,Updates,我调试了代码,然后在运行时customer正在更新(automapper正在工作,我可以看到更改),但是entity.SaveChanges()不工作 使用automapper时,是否有其他方法更新记录 提前感谢。您的代码创建了新的客户实体,该实体未附加到上下文: [HttpPost] public ActionResult _EditCustomer(CustomerViewModel CustomerViewModel) { if (ModelState.IsValid) {
entity.SaveChanges()代码>不工作
使用automapper时,是否有其他方法更新记录
提前感谢。您的代码创建了新的客户实体,该实体未附加到上下文:
[HttpPost]
public ActionResult _EditCustomer(CustomerViewModel CustomerViewModel)
{
if (ModelState.IsValid)
{
try
{
Customers customer = entity.Customers.FirstOrDefault(x => x.sno == CustomerViewModel.sno);
customer = AutoMapper.Mapper.Map<CustomerViewModel, Customers>(CustomerViewModel);
entity.SaveChanges();
return Content("<div class=\"success\">Müşteri düzenleme işlemi başarılı.</div>", "text/html");
}
catch (Exception e)
{
ModelState.AddModelError("", "Müşteri güncelleme hatası.");
}
}
//Updating customer is failed!
CustomerViewModel.Cities = entity.Cities;
CustomerViewModel.PowerSuppliers = entity.PowerSuppliers;
CustomerViewModel.Sectors = entity.Sectors;
return PartialView(CustomerViewModel);
}
您的代码将创建未附加到上下文的新客户实体:
[HttpPost]
public ActionResult _EditCustomer(CustomerViewModel CustomerViewModel)
{
if (ModelState.IsValid)
{
try
{
Customers customer = entity.Customers.FirstOrDefault(x => x.sno == CustomerViewModel.sno);
customer = AutoMapper.Mapper.Map<CustomerViewModel, Customers>(CustomerViewModel);
entity.SaveChanges();
return Content("<div class=\"success\">Müşteri düzenleme işlemi başarılı.</div>", "text/html");
}
catch (Exception e)
{
ModelState.AddModelError("", "Müşteri güncelleme hatası.");
}
}
//Updating customer is failed!
CustomerViewModel.Cities = entity.Cities;
CustomerViewModel.PowerSuppliers = entity.PowerSuppliers;
CustomerViewModel.Sectors = entity.Sectors;
return PartialView(CustomerViewModel);
}
Customers-customer=entity.Customers.FirstOrDefault(x=>x.sno==CustomerViewModel.sno);customer=AutoMapper.Mapper.Map(CustomerViewModel,customer);entity.SaveChanges()代码>对不起,我想问题是customerId=0,我使用hiddenFor,但它返回0错误:“缺少类型映射配置或不支持的映射。”您应该创建映射:Mapper.create()
另外,我会为CustomerViewModel
参数使用不同的名称,至少有一个是camel caseCustomerViewModel
代码>和这个:Mapper.CreateMap()
Customers=entity.Customers.FirstOrDefault(x=>x.sno==CustomerViewModel.sno);customer=AutoMapper.Mapper.Map(CustomerViewModel,customer);entity.SaveChanges()代码>对不起,我想问题是customerId=0,我使用hiddenFor,但它返回0错误:“缺少类型映射配置或不支持的映射。”您应该创建映射:Mapper.create()
另外,我会为CustomerViewModel
参数使用不同的名称,至少有一个是camel caseCustomerViewModel
代码>和这个:Mapper.CreateMap()代码>。
Mapper.Map(CustomerViewModel, customer);