EntityFramework. Delete all lines and add new ones. asynchronous

I am running CSV import into SQL db through an ASP.NET MVC application.

I repeat all the lines in the CSV, creating an equivalent object for each and adding each to the current context.

If there is at least one row to add, I want to delete all existing rows from the recordable table, and then commit the new objects.

My question is: is it safe to call ExecuteSqlCommandAsyncand not wait for the result before I then call await db.SaveChangesAsync();as follows:

db.Database.ExecuteSqlCommandAsync("delete from tblName");
await db.SaveChangesAsync();

Or should I awaitdelete a call before I make a change change call? In this case, can I also make calls for non-synchronous versions?

Currently, I do not expect this, and everything works as expected locally (almost without delay), that is, existing data is deleted and new data is added. It bothers me if there is anything else that I should consider when deploying the solution, and the web server and SQL server do not have to be in the same field.

+4
source share
2 answers

By MSDN :

By default, the DELETE statement always gets an exclusive (X) lock in the table being modified and holds that lock until the transaction completes.

This means that the save action will wait for the delete action to complete, even if the latter is started by async. So, since you are waiting for a save call, in fact you are also waiting for a delete call.

, delete ( async), (, , ).

: TRUNCATE TABLE.

+1

EF . . , EF, , .

EF, , , .

.

, . ? , . , .

+1

All Articles