Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework core: Cannot add migrations: No parameterless constructor

My data project reference: (Entity Framework Core)

    <Project Sdk="Microsoft.NET.Sdk">    
      <PropertyGroup>
        <TargetFramework>netcoreapp1.1</TargetFramework>
      </PropertyGroup>    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
        <ProjectReference Include="..\Butv.Core\Butv.Core.csproj" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" PrivateAssets="All" />      
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0" />    
      </ItemGroup>
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />  
      </ItemGroup>
      <ItemGroup>
        <Folder Include="Migrations\" />
      </ItemGroup>
    </Project>

The db Context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {  ....
     }
}
public class ApplicationUser : IdentityUser
{
}

Every time I try command

dotnet ef migrations add Init --verbose

, I get the error. It's used to work fine before I separate the Data project from the main project.

Microsoft.EntityFrameworkCore.Design.OperationException: No parameterless constr uctor was found on 'ApplicationDbContext'. Either add a parameterless constructo r to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'. ---> System.Mi ssingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo olean& bNeedSecurityCheck)

like image 709
nam vo Avatar asked Dec 05 '22 14:12

nam vo


1 Answers

I had the same problem. I have get it on the project that was migrates normally. From some point it start giving me this error. The problem was in Startup project selection. Select a project as startup in where you configured Entity Framework to use as a service.

This can help

like image 51
Yervand Abrahamian Avatar answered Mar 08 '23 23:03

Yervand Abrahamian