Entity Framework Tips(Part2)

Shayan Kamalzadeh
2 min readSep 3, 2021

--

I’m Shayan .Net developer, I’m going to talk about some simple tips that can help us to write a better query with EF.

In this part, I will talk about DELETE.

Let’s do it.

I have two classes that I want to use in the next parts

I want to delete a cat with ID 15

Therefore, first I find it and after that, I remove my object.

Run my SQL profiler:

Two queries run, first a select query after a delete query

Is it good? Absolutely not.

Why for each delete do we need a select query? It is awful.

Let's do it better:

I create an object from a cat and just set Id=16 and after that remove this object. without any finding

SQL profiler:

Just run delete query.

OK, my dudes, this is the second tip .please follow me and I’m going to talk more about simple tips.

--

--

Responses (2)