0
我有一個SaveClientDataMethod將保存,並根據我的數據庫首先我必須保存數據到地址和聯繫人表,並且只有更新或添加客戶端。那麼這裏是我的quastion,在這種情況下如何使用DbContext對象? Shoulld我在每種方法中創建上下文,或只在根方法或使用上下文作爲類的私有屬性?如何在EF中以正確的方式使用DBContext
public class TestClass
{
private void SaveClientDataMethod()
{
using (var context = new TestEntities)
{
NewRecord.FK_1 = TestMethod1(context);
NewRecord.FK_2 = TestMethod2(context);
context.TestTable.Add(NewRecord);
context.SaveChanges();
}
}
private int SaveAddress(TestEntities context)
{
context.SaveChanges(); /// Saving some data and returning id of new record
return id;
}
private int SaveContacts(TestEntities context)
{
context.SaveChanges(); /// Saving some data and returning id of new record
return id;
}
}
中,如果例外是在TestMethod2 accurs這樣的方式
此外,交易的一部分將被保存到數據庫,並違反UOW模式,請糾正我,如果我錯了。