[英]How can I delete records in Entity Framework using ExecuteSqlCommand?
This is my code:这是我的代码:
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))
{
foreach (ManufacturecodeEntity mcodeEntity in ManufacturecodeEntities)
{
ManufacturecodeEntity pcodeEntity = mcodeEntity.Parent;
pcodeEntity.IsCurrent = true;
UnitOfWork.ManufacturecodeRepository.Update(pcodeEntity);
}
UnitOfWork.DbContext.Database.ExecuteSqlCommand("Delete from manufacturecodes where detailstate_id=" + Id.ToString());
UnitOfWork.SaveChanges();
scope.Complete();
}
But when I run to method ExecuteSqlCommand
, my application stops, then throws timeout exception.但是当我运行到方法
ExecuteSqlCommand
,我的应用程序停止,然后抛出超时异常。
I use ExecuteSqlCommand
to delete records because records is more than 1500, if I use Entity Framework Delete
and SaveChanges
method, it will take 60s, I can't accept the result.我使用
ExecuteSqlCommand
删除记录,因为记录超过1500条,如果我使用Entity Framework Delete
和SaveChanges
方法,需要60s,我不能接受结果。
So I try the ExecuteSqlCommand
method to improve the performance.所以我尝试了
ExecuteSqlCommand
方法来提高性能。 Now it seems there is something wrong.现在好像有什么地方不对劲。
Please help me, thanks.请帮帮我,谢谢。
您应该在ExecuteSqlCommand
使用SqlParameters
作为:
UnitOfWork.DbContext.Database.ExecuteSqlCommand("Delete from manufacturecodes where detailstate_id=@id", new SqlParameter("@id", id);
I think I know correct way.我想我知道正确的方法。 I should use DbContext.Database.BeginTransaction instead of TransactionScope
我应该使用 DbContext.Database.BeginTransaction 而不是 TransactionScope
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.