我有一个WPF应用程序,其表单可以在一个Save按钮上单击更新多个相关表。我想将保存代码放入单独的函数中,如下所示。
这会有用吗?如何在一个ObjectContext和一个SaveChanges调用中执行此操作?
private void SaveEntity()
{
using (var oContext = new MyEntities())
{
SaveEntityInfo();
SaveOwners();
SavePartners();
SaveOfficers();
SaveDirectors();
oContext.SaveChanges();
}
}
答案 0 :(得分:1)
它可以用于一些更改。您可以这样更改代码:
private void SaveEntity()
{
using (var oContext = new MyEntities())
{
SaveEntityInfo(oContext);
SaveOwners(oContext);
SavePartners(oContext);
SaveOfficers(oContext);
SaveDirectors(oContext);
oContext.SaveChanges();
}
}
//One of multiple save methods
private void SaveEntityInfo(MyEntities context)
{
//Add something to context
//Remove something from context
//Update something in context
//But never use context.Save() here
//Let SaveChanges() only calls in your SaveEntity() method
}