这是我想做的:
var myCustomer = new Customer();
myCustomer.Name = "Bob";
myCustomer.HasAJob = true;
myCustomer.LikesPonies = false;
然后我想将它传递给一个更新方法:
public UpdateCustomer(Customer cust)
{
using(var context = dbcontext())
{
var dbCust = context.Customers.FirstOrDefault(c => c.Name == cust.Name);
if(dbCust != null)
{
// Apply values from cust here so I don't have to do this:
dbCust.HasAJob = cust.HasAJob;
dbCust.LikesPonies = cust.LikesPonies
}
context.SaveChanges();
}
}
这样做的原因是我在我的应用程序的多个不同部分和/或跨 DLL 工作。这可能吗?
编辑:发现这个问题非常有用: Update Row if it Exists Else Insert Logic with Entity Framework