So that I could keep my context clean and simple, I put a lot of logic in an abstract class and made my context inherit from it.
I saw this approach here but now since my class no longer inherits directly from DBContext I can’t create migrations.
My abstract class is
public abstract class MyContext : DbContext
{
public MyContext(string connString)
: base(connString)
{
}
public override int SaveChanges()
{
// custom code here
}
}
Now when I try and create an migration by typing add-migration at the PM console I get an error indicating that a class inheriting from DBContext cannot be found
The PM console shows
PM> add-migration kirsten2 No migrations configuration type was found in the assembly 'DataLayer'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
PM> Enable-Migrations No context type was found in the assembly 'DataLayer'.
If you search the EF repo for this error message (“No migrations configuration type was found”), you’ll find this resource in EntityFramework/Properties/Resources.cs file:
The next step would be search for
AssemblyMigrator_NoConfigurationusage, and you’ll findjust one occurence which is in EntityFramework/Migrations/Design/ToolingFacade.cs:
I think now it would be easier to track the error and fix it.
I’ve tested it on Source code and the message was irrelevant, turns out that the rel cause is a target framework tag in app.config.
Here is the similar question with right answer:
'Enable-Migrations' fails after upgrading to .NET 4.5 and EF 5
Interesting that If you run Enable-Mirations and Add-Migration pointing exactly to Context class name and Configuratin class name respectively, it works fine.
But the mentioned solution is the right solution and is easier also 🙂