我是实体框架的新手,我对在一个方法调用另一个方法时处理我的DbContext的正确方法感到困惑。这些都是独立调用的,这就是DeleteSheet是独立的原因。但是,当删除父(Chain)时,DeleteChain调用DeleteSheet删除子项。
我尝试在每个方法中处理,但是当我运行DeleteChain时,我得到:
The operation cannot be completed because the DbContext has been disposed.
在
db.Chains.Remove(chain);
我猜它是因为我在移除孩子时在DeleteSheet中处理了它。
以下是我的方法:
public class ControllerHelper
{
private UranusContext db = new UranusContext();
public void DeleteChain(int? chainId)
{
var chain = db.Chains.Include(c => c.Sheets)
.SingleOrDefault(c => c.ChainId == chainId);
// Removes sheets (children)
foreach (var sheet in chain.Sheets.ToList())
{
DeleteSheet(sheet.SheetId);
}
db.Chains.Remove(chain);
db.SaveChanges();
db.Dispose();
}
public void DeleteSheet(int? sheetId)
{
var sheet = db.Sheets.Include(s => s.FileDetails)
.Include(s => s.SheetsCounties)
.SingleOrDefault(s => s.SheetId == sheetId);
foreach (var fileDetails in sheet.FileDetails.ToList())
{
db.FileDetails.Remove(fileDetails);
}
foreach (var sheetsCounties in sheet.SheetsCounties.ToList())
{
db.SheetsCounties.Remove(sheetsCounties);
}
db.Sheets.Remove(sheet);
db.SaveChanges();
db.Dispose();
}
}
在这种情况下如何正确处理DbContext?我很困惑,因为如果我只处理DeleteChain然后在DeleteChain之外调用DeleteSheet,它就不会被处置。
编辑:尝试#2。我将每个包装在一个使用中并单独处理它们。但是,当我删除DeleteSheet
中的孩子时,DeleteChain
并没有意识到我这样做了,并在The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
db.SaveChanges()
DeleteChain
上投了public void DeleteChain(int? chainId)
{
using (UranusContext db = new UranusContext())
{
var chain = db.Chains.Include(c => c.Sheets)
.SingleOrDefault(c => c.ChainId == chainId);
// Removes sheets (children)
foreach (var sheet in chain.Sheets.ToList())
{
DeleteSheet(sheet.SheetId);
//db.Sheets.Remove(sheet);
}
db.Chains.Remove(chain);
db.SaveChanges();
}
}
public void DeleteSheet(int? sheetId)
{
using (UranusContext db = new UranusContext())
{
var sheet = db.Sheets.Include(s => s.FileDetails)
.Include(s => s.SheetsCounties)
.SingleOrDefault(s => s.SheetId == sheetId);
foreach (var fileDetails in sheet.FileDetails.ToList())
{
db.FileDetails.Remove(fileDetails);
}
foreach (var sheetsCounties in sheet.SheetsCounties.ToList())
{
db.SheetsCounties.Remove(sheetsCounties);
}
db.Sheets.Remove(sheet);
db.SaveChanges();
}
}
。 / p>
尝试#2。
$
答案 0 :(得分:1)
您可能希望在单独的方法中移动工作表删除逻辑,并从DeleteChain和DeleteSheet中调用它,如下所示:
public class ControllerHelper
{
public void DeleteChain(int? chainId) {
using (var db = new UranusContext()) {
var chain = db.Chains.Include(c => c.Sheets)
.SingleOrDefault(c => c.ChainId == chainId);
// Removes sheets (children)
foreach (var sheet in chain.Sheets.ToList()) {
DeleteSheet(db, sheet);
}
db.Chains.Remove(chain);
db.SaveChanges();
}
}
public void DeleteSheet(int? sheetId) {
using (var db = new UranusContext()) {
var sheet = db.Sheets.Include(s => s.FileDetails)
.Include(s => s.SheetsCounties)
.SingleOrDefault(s => s.SheetId == sheetId);
DeleteSheet(db, sheet);
db.SaveChanges();
}
}
private void DeleteSheet(UranusContext db, Sheet sheet) {
foreach (var fileDetails in sheet.FileDetails.ToList()) {
db.FileDetails.Remove(fileDetails);
}
foreach (var sheetsCounties in sheet.SheetsCounties.ToList()) {
db.SheetsCounties.Remove(sheetsCounties);
}
db.Sheets.Remove(sheet);
}
}
答案 1 :(得分:0)
最简单的方法是将DbContext作为参数传递:
public class ControllerHelper
{
public void DeleteChain(int? chainId, UranusContext db)
{
var chain = db.Chains.Include(c => c.Sheets)
.SingleOrDefault(c => c.ChainId == chainId);
// Removes sheets (children)
foreach (var sheet in chain.Sheets.ToList())
{
DeleteSheet(sheet.SheetId, db);
}
db.Chains.Remove(chain);
}
public void DeleteSheet(int? sheetId, UranusContext db)
{
var sheet = db.Sheets.Include(s => s.FileDetails)
.Include(s => s.SheetsCounties)
.SingleOrDefault(s => s.SheetId == sheetId);
foreach (var fileDetails in sheet.FileDetails.ToList())
{
db.FileDetails.Remove(fileDetails);
}
foreach (var sheetsCounties in sheet.SheetsCounties.ToList())
{
db.SheetsCounties.Remove(sheetsCounties);
}
db.Sheets.Remove(sheet);
}
}
然后像
一样使用它public void Foo(int? someChainId, int? someSheetId, bool required = false)
{
ControllerHelper hlp = new ControllerHelper();
using (UranusContext db = new UranusContext())
{
hlp.DeleteChain(someChainId);
if(_required)
hlp.DeleteSheet(int? someSheetId);
db.SaveChanges();
}
}
答案 2 :(得分:0)
尝试使用模式并将上下文作为参数传递给DeleteSheet方法:
public class ControllerHelper
{
public void DeleteChain(int? chainId)
{
using (UranusContext db = new UranusContext())
{
var chain = db.Chains.Include(c => c.Sheets)
.SingleOrDefault(c => c.ChainId == chainId);
// Removes sheets (children)
foreach (var sheet in chain.Sheets.ToList())
{
DeleteSheet(sheet.SheetId, db);
}
db.Chains.Remove(chain);
db.SaveChanges();
}
}
public void DeleteSheet(int? sheetId, UranusContext db)
{
var sheet = db.Sheets.Include(s => s.FileDetails)
.Include(s => s.SheetsCounties)
.SingleOrDefault(s => s.SheetId == sheetId);
foreach (var fileDetails in sheet.FileDetails.ToList())
{
db.FileDetails.Remove(fileDetails);
}
foreach (var sheetsCounties in sheet.SheetsCounties.ToList())
{
db.SheetsCounties.Remove(sheetsCounties);
}
db.Sheets.Remove(sheet);
db.SaveChanges();
}
}
这样,您可以确保仅在完成所有删除后才处理上下文。我总是试图避免一次使用多个上下文,我之前已经被它严重烧伤了!
答案 3 :(得分:0)
在您的设计中,ControllerHelper
是UranusContext
的组合。这意味着您设计的只要ControllerHelper
存在,UranusContext
就存在,并且您希望UranusCibtext
在ControllerHelper
不再存在时立即停止存在
通过将其作为一个合成,您可以向您的班级用户承诺,只要您的班级存在,您的所有功能都会起作用。因此,在用户通知您不再需要您的功能之前,您不应Dispose()
UranusContext
任何功能。这可以通过制作ControllerHelper
IDisposable
如果任何类实现了IDisposable,则此类的设计者鼓励用户在不再需要该对象的功能后尽快调用Dispose()
。
在您的情况下,代码将是:
public class ControllerHelper : IDisposable()
{
private UranusContext db = new UranusContext();
public void Dispose()
{
if (this.db != null)
{
this.db.Dispose();
this.db = null;
}
GC.SuppressFinalize(this);
}
public void DeleteChain(int? chainId)
{
// Do what you need to do, but don't Dispose()
// nor use using, we want db to be functional
}
public void DeleteSheet(int? sheetId)
{
// Do what you need to do, but don't Dispose()
// nor use using, we want db to be functional
}
}
用法是:
using (var controllerHelper = new ControllerHelper())
{
// call functions from controllerhelper
}
// the controllerHelper is not needed anymore, Dispose() is called automatically
// and thus db is Disposed