【发布时间】:2012-01-12 07:28:20
【问题描述】:
这是一个具体的问题。我们正在使用 C#、EF4.1 和 Mapper 编写 wcf 服务。 我们不想使用存储过程……无论如何;问题是,我们要编辑 db 上的地址字段。但是我们无法将编辑后的地址保存到数据库中。
public int EditAddress(int userId, Address address)
{
using(var mobileServiceContext = new MobileServiceContext())
{
Address oldAddress = mobileServiceContext.Addresses.FirstOrDefault(p => p.UserId == userId && p.AddressId == address.AddressId);
Address lastAddress = Mapper.Map(address, oldAddress);
//save new-last address with ? HOW ?
//mobileServiceContext.Addresses.Attach(updatedAddress); //doesn't work
mobileServiceContext.SaveChanges();
}
return address.AddressId;
}
这是我们编辑的函数;
public int EditAddress(int userId, Address address)
{
using(var mobileServiceContext = new MobileServiceContext())
{
Address oldAddress = mobileServiceContext.Addresses.FirstOrDefault(p => p.UserId == userId && p.AddressId == address.AddressId);
Address lastAddress = Mapper.Map(address, oldAddress);
mobileServiceContext.Addresses.Attach(lastAddress); //error on this line
mobileServiceContext.ObjectStateManager.ChangeObjectState(lastAddress, EntityState.Modified);
mobileServiceContext.SaveChanges();
}
return address.AddressId;
}
注意:“地址地址”类已经有 address.Id 字段。
我们在这里制作的设计非常复杂,读者也很难阅读。
【问题讨论】:
标签: c# wcf entity-framework-4 automapper