This is a specific question. We are coding a wcf service with using C#, EF4.1 and Mapper. We don t want to use stored procedures... Anyway; Problem is, we want to edit address field on db. But we can t save edited address to 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;
}
说明:“地址”类别已经处理。 同上。
我们在这里所做的设计非常复杂,也难以读到读者。