EFCorePowerTools icon indicating copy to clipboard operation
EFCorePowerTools copied to clipboard

Using multiple DbContexts in the same project

Open JimF42 opened this issue 2 years ago • 3 comments

I have a project that needs to use multiple DbContexts in the same project. I have put the cs files into separate folders, but I prefer to keep the namespaces simpler and have overridden them via the UX.

I now end up with multiple DbContextExtensions.Sync.cs files that cause errors. I have to currently delete one of them or change it's build action to None.

Would it be possible to either have EF Core Power Tools "detect" if one already existed and not create a new one, or, simply add an Advanced option to suppress the DbContextExtensions from being created? Either of these options would make it less confusing to other developers who have to maintain this code after I'm gone.

Provide steps to reproduce

Add multiple DbContexts to a project, but point them to the same namespace (different folders though).

Provide technical details

  • EF Core version in use: EF Core 6

  • Is Handlebars used: no

  • Is .dacpac used: no

  • EF Core Power Tools version: 2.5.1068

  • Database engine: SQL Server

  • Visual Studio version: Visual Studio 2022

JimF42 avatar Aug 11 '22 14:08 JimF42

But they have namespaces??

You have made them the same??

ErikEJ avatar Aug 11 '22 15:08 ErikEJ

Correct. They have the same namespaces. .Data and .Models. (That's why you let us override them in the UX, right?) I'm not looking for the extra typing and visuals of .Data.ThisDb and .Data.ThatDb. I can see that they are different databases simply by using a different variable name for each of the dbContexts that get created.

In my scenario, I will never have overlapping model names to deal with, so putting them all into the same namespace is cleaner for me.

JimF42 avatar Aug 11 '22 15:08 JimF42

Please share a sample project and I will have a think about what can be done.

Sounds a little like an edge case with a lot of anti patterns...

ErikEJ avatar Aug 11 '22 16:08 ErikEJ

Ping?

ErikEJ avatar Aug 13 '22 06:08 ErikEJ

You can remove the duplicate file using efpt.postrun.cmd https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#saving-options-and-running-the-second-time

ErikEJ avatar Aug 14 '22 11:08 ErikEJ

Sorry for the delay in getting back to you with a sample project. I was trying to figure out what two databases you would have access to to try it against.

As for your last comment on using efpt.postrun.cmd, I am perfectly fine with this solution. It suites my needs.

Thank you for your time on this.

JimF42 avatar Aug 15 '22 12:08 JimF42