Something is seriously wrong with EF, it doesn’t know how to work this out and find what it needs for some reason…
Using EF 8.
Packages:
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
Running
dotnet ef migrations add Initial --project=App.Database
Results in
Unable to create a ‘DbContext’ of type ”. The exception ‘Object
reference not set to an instance of an object.’ was thrown while
attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
My DbContext and entites are in App.Database
. I have no Program.cs in here or any bootstrapping code for DI or IOC container, so how does it know how to create it?
I’ve made a DbContextFactory but its just getting ignored.
Context:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options)
{
}
}
Context factory:
public class MyContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseMySql("Server=127.0.0.1;Database=...;Uid=root;Pwd=...;", MariaDbServerVersion.LatestSupportedServerVersion);
return new MyContext(optionsBuilder.Options);
}
}