How Entity Framework Tries to Copy Dapper in .NET 8

How Entity Framework Tries to Copy Dapper in .NET 8

Nick Chapsas

1 год назад

41,421 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Nick Chapsas
Nick Chapsas - 13.04.2023 14:34

I accidentally cut the part explaining it but the reason why I am testing FormattableString later in the video is to see if the generated templates are interned/optimized to prevent future allocations.

Ответить
Sohail Ajaz
Sohail Ajaz - 29.09.2023 16:12

Hi Nick, did you try a benchmark using compiled query in EF8. Thanks!

Ответить
Mohamed Cisse
Mohamed Cisse - 20.09.2023 00:23

No matter what , I think Dapper is still faster.

Ответить
Mahdi Malekan
Mahdi Malekan - 14.09.2023 12:22

The FormattableString is similar to Serilog string format. Is this correct?

Ответить
Justice Freeman
Justice Freeman - 25.06.2023 19:10

"Hello, everybody! I'm Naked" - I hear it all the time..

Ответить
Azaraba
Azaraba - 28.04.2023 23:48

Before LINQ and Expressions Trees we used strings. I dont want to return to that practices

Ответить
Emery Noel
Emery Noel - 28.04.2023 18:10

I'm kind of curious to see how the `FormattableString` would handle an nvarchar param. I can't imagine it would put quotes around it? But a parameterized query certainly would.

Ответить
DAoC Nostalgia
DAoC Nostalgia - 22.04.2023 21:05

The constructor injecting dbcontexts into repos is also how EF is presented in tutorials so I can’t blame the MS shops, they are just following instructions. It’s the safest way to use EF as well, so I don’t blame them either.
However I think they should add an additional AsConcurrentNoTracking fluent option, which would effectively use a separate new context behind the scenes instead of the repo scoped context.
The we could see dramatic performance improvements on complex workflow data fetching speeds

Ответить
DAoC Nostalgia
DAoC Nostalgia - 22.04.2023 20:54

For me the best thing about dapper is I can fetch all potentially relevant data at the beginning of a large workflow and only wait on it right before I actually need it.
Although you can technically do this with EF, most MS shops inject a db context into the repo class and use only 1 dbcontext per repo instance, which prevents you from using concurrent queries like you always can in dapper

Ответить
DAoC Nostalgia
DAoC Nostalgia - 22.04.2023 20:48

String interpulation has already been fixed in the compilation stage, you don’t need to turn your strings into templates anymore the compiler does that now

Ответить
Perra
Perra - 20.04.2023 14:38

nhibernate is better anyway ....

Ответить
Rayan
Rayan - 19.04.2023 09:42

EF all the way

Ответить
Donald Reynolds
Donald Reynolds - 18.04.2023 01:53

It seems there is a big difference between .FromSql() which uses sql parameters and .FromSqlRaw() which uses string interpolation. Why not test both of those methods as well?

Ответить
Pratik {Binga}
Pratik {Binga} - 15.04.2023 14:18

Hi Nick,
Thanks for this Awesome video :)
Is there any discount coupon code for purchasing course from your website?

Ответить
Eike Stein
Eike Stein - 14.04.2023 14:41

Dapper and EF Core have simply different use cases. Use dapper when performance is critical and use EF Core when it is not and you rather have the better development experience of Linq-to-SQL.

Ответить
Slipoch
Slipoch - 14.04.2023 02:13

As far as I am concerned EF has never really been fit for purpose, it is overweight, slow , and very limited.

It also involves a non-model first approach by forcing you to manually generate migrations using command line when using model-first. Then manually tell it to update the db. From my POV that is a DB-first approach that just happens to work a bit differently.

It also requires a lot of code for a simple system.

Give me NoSQL like VelocityDB or Realm any day, but if you have to use SQL (and in some cases SQL will be the better choice) then a lightweight ORM or something like dapper is a much better choice than EF.

Ответить
Bobo Mescalinum
Bobo Mescalinum - 14.04.2023 01:26

DbSet property is not required in your DbContext. If you just want IQueryable<T> you can use the Set<T> function of your DbContext

Ответить
Sinus32
Sinus32 - 13.04.2023 23:32

Dapper does not do any "String.Format" things. It doesn't read the provided SQL at all. It passes SQL and parameters as they are, down to db provider.
On the other hand, EF methods FromSql and FromSqlInterpolated first have to change "SELECT * FROM Movies WHERE Id={0} LIMIT 1" into something like "SELECT * FROM Movies WHERE Id=@p01 LIMIT 1" and prepare a parameter for db. And here I have a small warning about FromSqlRaw, because this one doesn't do this and hence is vulnerable to SQL injection attacks, if improperly used.

Ответить
mcnets
mcnets - 13.04.2023 22:51

Hi Nick first thanks for your job. I never used EF, I feel more comfortable working with Dapper and stored procedures. It give a way to make some changes without even touch my code

Ответить
AmirHossein Ahmadi
AmirHossein Ahmadi - 13.04.2023 22:22

While this is all definitely interesting, it's worth noting that this sort of thing is hardly ever relevant in real-world applications.
The actual bottleneck of your app, performance-wise, is almost invariably going to be the queries themselves and the time it takes for the database engine to actually execute them and return the results.

Obsessing over a few microseconds here and there isn't very productive. The DX benefits you get out of an elegant ORM like EF normally far outweigh those relatively insignficiant performance gains you might get by opting for a lower-level tool like Dapper, at least in most scenarios.

So, take these benchmarks and so on with a grain of salt. Although they're interesting, this type of thing shouldn't really be what ultimately informs your decision on which tool you should be using for your project.
Remember: "Premature optimization is the root of all evil."

Ответить
Dline8
Dline8 - 13.04.2023 21:59

Thanks for all the great informative content Nick! Been a great help in my professioinal career so far. Would you be able to do a video on why yo prefer not to use stored procedures? I don't have a dog in (what is apparently) this fight, I just have seen(and had to work with) stored procedures during my career and wanted more insight on thefrom someone who has more architectural/Devops knowledge than I do!

Thanks again!

Ответить
powermetal1963
powermetal1963 - 13.04.2023 21:32

Using EF Core with raw SQL makes no sense as it kills almost all ORM benefits. Too bad they are working on this rather than improving LINQ query translator, adding window function support to it etc.

Ответить
Adrian
Adrian - 13.04.2023 18:43

Does EF 8 needs .NET 8?

Ответить
Reza Mohamadzadeh
Reza Mohamadzadeh - 13.04.2023 18:00

Thanks Nick. It was amazing and useful.
So we can use ef core instead of dapper in this senario👍

Ответить
Viktor Sorokovikov
Viktor Sorokovikov - 13.04.2023 16:06

For me is more interesting to call SP, for both update and request scenarios. And do it in scope of one transaction, Dapper is very good with it.

Ответить
Ivan Tatarchuk
Ivan Tatarchuk - 13.04.2023 16:04

Very unintuitive result.
Nick, you should dig dipper, plz :)
The only guess (without actual doc reading/debugging) is that this query is not so "detached" from the db context.

Ответить
Jim B
Jim B - 13.04.2023 15:34

I thought it wasn't called 'Core' anymore?

Ответить
Pi Tallano
Pi Tallano - 13.04.2023 15:02

Hi Nick, What do you think of RepoDb?

Ответить
Pavel Voronin
Pavel Voronin - 13.04.2023 14:57

I am a bit disappointed that EF is positioned as a competitor to Dapper. Marketing a feature-rich ORM as a tool for ETL seems to me not the best idea. Its strongest part should be mapping DB structures to complex domain objects and change tracking (if needed), that is I'd put more emphasis on DDD-like things.

Ответить
Während du diesen langen Namen liest habe ich deine Kekse geklaut.
Während du diesen langen Namen liest habe ich deine Kekse geklaut. - 13.04.2023 14:55

Nice that you made another video to compare both! What's your guess on why Dapper is still so much faster? What's happening behind the scenes?

Ответить
codingbloke
codingbloke - 13.04.2023 14:51

I would imagine the the EF8 has to create a parameterised version of the SQL string, then create a command with parameters. Whereas in Dapper you have done the work of creating the the parameterised SQL, already. Dapper can use the SQL constant you have supplied as is.

Ответить
Hemera
Hemera - 13.04.2023 14:38

Happy 30th birthday Nick!

Ответить
Andrii
Andrii - 13.04.2023 14:38

in EF you can map query to objects like this: dbContext.Set<Model>().FromSqlRaw("select * ...") but you need to have DbSet<Mode> added, you also can ignore this table from creating in DB.

Ответить
reikooters
reikooters - 13.04.2023 14:36

I still can't ever see myself using an ORM such as EF, but nice to see they're getting some of the features that it's missing.

Ответить
Uwe Keim
Uwe Keim - 13.04.2023 14:31

"Good is often good enough"

Ответить
Stephan Haller
Stephan Haller - 13.04.2023 14:30

1st

Ответить