A painful introduction
I’ll keep this one short as I currently need to figure out a way to stick my hair back on the my head, after wasting the last hour tearing it all out.
Basically, I’m currently in the middle of building a new side project using .NET Core. I’m trying to apply a Clean Architecture approach to it, avoiding your typical repository pattern approach but utilising EF Core with Identity as my persistence layer.
Resultantly, this therefore meant that my migrations and DbContext had to live in a separate project to my Web API project which acts as a presentation layer and this caused some hiccups which I’ll try to point out below.
Firstly, we must ensure that our AddDbContext in our Startup.cs class of our Web API project is pointing at the separate project which contains our actual DbContext implementation.
dotnet ef migrations add InitialMigrations
Usually when our DbContext exists within the same project as our Startup file, it takes a couple of seconds to generate and add our initial migrations via the awesome CLI that gets shipped with EF Core.
However, in this scenario, I ran in to a couple of issues:
After running the migrations add command in my Web API project…
So this error was confusing as it was clear that it had picked up that my DbContext lived in a separate project. But fortunately it pointed me in the direction of changing my target project in the Package Manager console. So I did.
Unfortunately after changing my target project/root directory in regular cmd/bash, this resulted in a separate error…
I was baffled, so I tried to add an OnModelCreating() override within my IdentityDbContext inheriting DbContext to see if it would help and to an extent it did. To a certain extent…
But whilst it helped, it just ended up digging me down a deeper hole.
So, the solution?
I had to go up a level to the parent of both projects and then specify both as shown above. A startup project and another project which specifies where the DbContext in this case lives. Straight away after running this, just by magic, my migrations were generated and I resultantly followed it up with a dotnet ef database update.
This isn’t much and it might be obvious to some, but judging from the hoarding of googling that I was going through, it was quite a common issue without a specific one size fits all solution that is easy to find online. So hopefully this helps some other poor sod out there.
Thanks for reading!