2

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.

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.

So I try the ExecuteSqlCommand method to improve the performance. Now it seems there is something wrong.

Please help me, thanks.

2 Answers 2

2

You should use SqlParameters in the ExecuteSqlCommand as:

UnitOfWork.DbContext.Database.ExecuteSqlCommand("Delete from manufacturecodes where detailstate_id=@id", new SqlParameter("@id", id);
Sign up to request clarification or add additional context in comments.

1 Comment

same result, time out exception
0

I think I know correct way. I should use DbContext.Database.BeginTransaction instead of TransactionScope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.