DON'T Use DbContext In Blazor Interactive Server Components! (.NET8)

DON'T Use DbContext In Blazor Interactive Server Components! (.NET8)

Codewrinkles

8 месяцев назад

6,996 Просмотров

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


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

@user-fqlt
@user-fqlt - 28.12.2023 16:59

What about using dbContext with ServiceLifetime.Transient ?

Ответить
@naunihalsidhu
@naunihalsidhu - 14.12.2023 17:58

First of all great video highlighting the DbContext issue in Blazor Server(.NET 6/7) and now in .NET for Interactive Server Side Rendering.
I encountered it back in .NET 6 and was able to resolve it back than using the technique you mentioned here.
I know it takes lot of time to create video and sharing it for the community, great work and great topic !.
I also noticed that every time we run EF query or any operation on EF we should always use await ... EF Operation, otherwise it can run into threading issue.

Ответить
@sinandoganli
@sinandoganli - 09.12.2023 23:11

If we do not use Ef and DBcontext, but perform our operations using system.data.sqlclient with a helper we wrote and dispose at the end of the transaction, will we encounter the same problem?

Ответить
@MrXLR82
@MrXLR82 - 08.12.2023 23:36

Is it efficient to create db context everytime?

Ответить
@TosoAlejandro
@TosoAlejandro - 26.11.2023 21:49

Thank you for your video! I found the solution to my issue with DbContext in Blazor Interactive Server. It worked perfectly with the solution from your video. I had no idea what could be causing the error in my application; it was functioning correctly until it suddenly stopped, throwing some very strange errors. I had no clue until I started searching online and happened to come across your video. Great help! 👏🏽😄

Ответить
@drumistoyanov4075
@drumistoyanov4075 - 23.11.2023 13:22

I was having this trouble a few days ago. Wasted 5 hours to find out what was the problem...

Ответить
@luciannaie
@luciannaie - 21.11.2023 02:02

I wonder if it gets fixed when the scope is set globally: options.UseSqlServer("", ServiceLifetime.Transient);

Ответить
@krutelut
@krutelut - 19.11.2023 20:00

amazing video!
Is it possible to get the source code for this small project of yours :)

Ответить
@marcusmaunula5018
@marcusmaunula5018 - 19.11.2023 19:30

What about using Connection Pooling? Have you done a video on that?

Ответить
@ProDevelopmentPK
@ProDevelopmentPK - 19.11.2023 09:53

Nice, I've a Global InteractiveServer App which uses Repository Pattern and Services. I Inject IUserService in my Component and that Service use UnitOfWork that handles DbContext. I've faced exceptions while navigating to multiple components that uses DbContext.

Do you think If I just replace builder.Services.AddDbContext to AddDbContextFactory then it will resolve my issue?

Ответить
@patrickkuhnel9531
@patrickkuhnel9531 - 18.11.2023 02:44

I inject the service provider to my blazor components and manually create scopes in my components and get the services from these scopes. Are there any drawbacks with this? I don't like the idea of changing Backend code because some frontends behave different than others. The issue is not the Backend it's the frontend, so that's where I fix it.

Ответить
@saroshwadia
@saroshwadia - 17.11.2023 20:23

Would DbContextFactory work the same in Server/WASM/SSR - so we can use it in all types of Blazor Apps? Thx great video

Ответить
@45g4rerf45f45
@45g4rerf45f45 - 17.11.2023 17:40

Im using such helper, basically to don't keep entities tracked:

public class HelperService
{
public void InNewDbCtx(Action<MyDataContext> func)
{
using var dbContext = new MyDataContext();
func(dbContext);
}
}

and then HelperService.InNewDbCtx(db => ...do stuff.. );


Maybe fetching data with AsNoTracking would also help

Ответить
@marianf25
@marianf25 - 17.11.2023 16:36

If I would have seen this video 3 years ago I would now have in total at least 1 week of my life back 🤣 btw I'm curious how do you handle repositories/services with this approach?

Ответить
@ClaudioBernasconi
@ClaudioBernasconi - 17.11.2023 16:33

Interesting video. I haven't used EFCore with Blazor so far, but I will keep it in mind for future projects. It's great to know. Thanks.

Ответить
@richardaubin1951
@richardaubin1951 - 17.11.2023 14:11

I once had a scenario where IDbContextFactory wasn't suitable, so we decided to use IServiceScopeFactory and achieved the same result.

Ответить
@user-dc9zo7ek5j
@user-dc9zo7ek5j - 17.11.2023 14:01

I really like your videos but... you seem to repeat the same topics over and over, each time adding only small piece that is irrelevant to the whole picture.
The simplest and cleanest way to use DI is to use... IServiceProvider, yes I know people say this is an antipattern, but, when you're working outside the request response lifecycle (MVC, Razor Pages), and instead are working with a single long lived session (Blazor, HostedService), then you need to create the scope yourself and dispose it when needed, this will ensure that all services work properly and there isn't an issue with concurrently doing 2 operations at once, another benefit is that you can make transactions across services because you have the full control of the scope.

Ответить