EF6 - 有没有办法清除EF缓存/本地项目而不会对数据库造成任何改变? [英] EF6 - Is there a way to clear EF cache / Local items without causing any change into the database?
问题描述
这里是一些伪代码方案(我现在有更复杂的代码):
public void RemoveCustomer(int customerUID)
{
//这个代码在一个db事务中运行
{
//使用EF
var orders = repoOrders.GetOrdersOfCustomer(filter,customerUID)检索特定客户的某些订单;
//在使用EF
repoX.DoSomethingWithOrders(orders)之前,使用上面的订单执行某些操作。
//调用SP删除客户的所有订单
repoOrders.DeleteAllOrdersOfCustomer(customerUID); //这调用一个存储过程
//删除客户使用EF
repoCustomers.DeleteCustomer(customerUID); //由于关系抛出异常!
}
}
当然,客户订单是一对多
(1:m)关系。
我想避免在上述情况下抛出异常将会有一些下载属于客户的上下文被删除。例外是:
由于一个或多个
外键属性不可为空,因此无法更改关系
如果外键不支持空值,则新的关系
必须被定义的外键属性必须分配另一个
非空值,否则不相关的对象必须被删除。
所以,我想知道是否可以从< DbSet> .Local
中清除某些/所有订单,而不会在调用存储过程后导致对数据库的任何更改在用户被删除之前。
我猜想 Detach 可以使用,但这意味着我应该循环使用。
你会推荐什么?
编辑:我是EF的新手,我现在在使用ADO.NET完成存储库之后集成EF,是的, BL一直保持不变,所以我在这一点上尝试这种融合,尽量少。
注意:我不能更改数据库结构。
我亲自使每个存储库方法都使用自己的上下文,将其自己的更改保存在结束方法等,然后使用 TransactionScope
确保操作是原子的。
void DeleteAllOrdersOfCustomer(Guid customerUID)
{
using(var context = new Context())
{
...
context.SaveChanges();
}
}
...
using(var ts = new TransactionScope())
{
//调用SP删除客户的所有订单
repoOrders .DeleteAllOrdersOfCustomer(customerUID); //这调用一个存储过程
//删除客户使用EF
repoCustomers.DeleteCustomer(customerUID); //由于关系抛出异常!
ts.Complete();
}
Basically I am mixing EF with one call to a stored procedure which does some batch deletions, otherwise EF is too slow.
Here is some pseudo-code of this scenario (I have more complex code in reality):
public void RemoveCustomer(int customerUID)
{
// this code is running in one db transaction
{
// retrieve certain orders of particular customer using EF
var orders = repoOrders.GetOrdersOfCustomer(filter, customerUID);
// do something with above orders before deletion using EF
repoX.DoSomethingWithOrders(orders);
// call SP to delete all orders of customer
repoOrders.DeleteAllOrdersOfCustomer(customerUID); // this calls a stored procedure
// delete customer using EF
repoCustomers.DeleteCustomer(customerUID); // throws exception due to relationship!
}
}
of course, customers-orders is one-to-many
(1:m) relation.
I want to avoid in the above scenario the exception which is thrown when there will be some orders loaded by the context belonging to the customer that gets deleted. Exception is:
"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."
So, I want to know if is possible to clear some/all orders from <DbSet>.Local
without causing any change into the database after calling the stored procedure and before user gets deleted.
I guess Detach could be used, but this means I should loop through.
What would you recommend?
Edit: I am new to EF and I am integrating EF now after the repositories were done using ADO.NET, and yes, the BL has been kept the same... so I try this integration with minimal efforts at this point.
Note: I can't make changes on the database structure.
I'd personally make each repository method use its own context, save its own changes at the end of the method, etc., and then use a TransactionScope
to ensure that the operations are atomic.
void DeleteAllOrdersOfCustomer(Guid customerUID)
{
using (var context = new Context())
{
...
context.SaveChanges();
}
}
...
using (var ts = new TransactionScope())
{
// call SP to delete all orders of customer
repoOrders.DeleteAllOrdersOfCustomer(customerUID); // this calls a stored procedure
// delete customer using EF
repoCustomers.DeleteCustomer(customerUID); // throws exception due to relationship!
ts.Complete();
}
这篇关于EF6 - 有没有办法清除EF缓存/本地项目而不会对数据库造成任何改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!