I am working with the latest .Net Core and EF Core in Visual Studio 2017. I have created a model and it was working great. I have since made some modifications and am gett
If you have a separate project for context. set a startup project with the target project
dotnet ef migrations add InitialCreate -s .\src\WebUI\ -p .\src\Infrastructure\ --verbose
I had the same issue. My problem started when i changed my imeplementation of ApplicationUser to User with Guid Id.
public class User : IdentityUser<Guid>
First I started getting some crazy errors and after setting my DbContext to:
public class NgSchoolsContext : IdentityDbContext<User, IdentityRole<Guid>, Guid>
I started getting the "No parameterless constructor" error. What finally fixed it was changing the Roles configuration in Startup:
services.AddIdentityCore<User>()
.AddEntityFrameworkStores<NgSchoolsContext>()
.AddRoles<IdentityRole<Guid>>()
.AddDefaultTokenProviders();
Notice the IdentityRole part. You don't need to implement a thing, just put it like that. It should work out of the box. HOWEVER!! Your migrations WILL fail and you'll have to rebuild your db from start. Entity can't handle the fact that all of his primary keys went from string(or whatever) to Guid and won't be able to update the database. Well, that is at least easier to solve. Drop database, delete all migrations, Add-Migration (new initial) and Update-Database. This would be so easy if for normal error message.
I had the same error "No parameterless constructor defined for type ..." while trying to adda migration.
Turns out that when running ef migrations
entity framework passes on arguments in the startup project Program.cs
The string[] args
was removed for security reasons. Adding this resolved the issue.
Design-time tools attempt to automatically find how your application creates instances of your DbContext type. If EF cannot find a suitable way to initialize your DbContext, you may encounter this error.
Options: 1- Either create a parameterless constructor
public AlmanacDb() { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connString);
}
private readonly string _connString = "<your conn string>";
2-
public AlmanacDb Create()
{
var optionsBuilder = new DbContextOptionsBuilder<AlmanacDb>();
optionsBuilder.UseSqlServer(connectionString);
return new AlmanacDb(optionsBuilder.Options);
}
https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext