Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am having doubts while learning DbContextFactory #32

Open
shouhujishu opened this issue Jan 13, 2021 · 1 comment
Open

I am having doubts while learning DbContextFactory #32

shouhujishu opened this issue Jan 13, 2021 · 1 comment

Comments

@shouhujishu
Copy link

shouhujishu commented Jan 13, 2021

Hello. I am having doubts when using DbContextFactory. Is there any difference between CreateDbContext() and new DbContext()?
Is there any difference between using DbContextFactory and using(=new DbContext())? Thank you, can you tell me?

@shouhujishu shouhujishu changed the title Hello, I can’t understand DbContextFactory. Can you simply answer it for me? I am having doubts while learning DbContextFactory? Jan 13, 2021
@shouhujishu shouhujishu changed the title I am having doubts while learning DbContextFactory? I am having doubts while learning DbContextFactory Jan 13, 2021
@andreyeurope
Copy link

@shouhujishu I don't know if you are still learning EF Core, but after spending a good amount of time, it seems there is really no best solution.
If you create a new instance each time, sure, you might escape concurrency problems, but you lose other properties of EF Core, like Caching.
And then if you want to span a DB transaction across multiple services, how do you do that? I am not an expert myself, but I would say you CAN'T. You'd have to pass around your original context and chose a point where you'd want to commit your transaction, or roll it back if you have any errors.
Each approach has pros and cons, so you'd have to make the choice yourself. I don't think there's any bad approach (putting aside using the same DbContext within multiple threads).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants