Entity Framework Core - Migration - No Parameterless Constructor Defined for this Object

前端 未结 4 1785
野的像风
野的像风 2021-01-11 22:19

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

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-11 23:18

    I had the same issue. My problem started when i changed my imeplementation of ApplicationUser to User with Guid Id.

    public class User : IdentityUser

    First I started getting some crazy errors and after setting my DbContext to:

    public class NgSchoolsContext : IdentityDbContext, Guid>

    I started getting the "No parameterless constructor" error. What finally fixed it was changing the Roles configuration in Startup:

    services.AddIdentityCore() .AddEntityFrameworkStores() .AddRoles>() .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.

提交回复
热议问题