简体   繁体   English

实体框架核心:无法添加迁移:无参数构造函数

[英]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 dotnet ef migrations添加Init --verbose

, I get the error. ,我收到了错误。 It's used to work fine before I separate the Data project from the main project. 在将Data项目与主项目分开之前,它已经习惯了。

Microsoft.EntityFrameworkCore.Design.OperationException: No parameterless constr uctor was found on 'ApplicationDbContext'. Microsoft.EntityFrameworkCore.Design.OperationException:在“ApplicationDbContext”上找不到无参数构造函数。 Either add a parameterless constructo r to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'. 将无参数构造添加到'ApplicationDbContext'或在与'ApplicationDbContext'相同的程序集中添加'IDbContextFactory'的实现。 ---> System.Mi ssingMethodException: No parameterless constructor defined for this object. ---> System.Mi ssingMethodException:没有为此对象定义无参数构造函数。 at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo olean& bNeedSecurityCheck) 在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOn ly,Boolean noCheck,Boolean&canBeCached,RuntimeMethodHandleInternal&ctor,Bo olean&bNeedSecurityCheck)

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 这可以帮助

It's exactly like the error says. 这与错误说的完全一样。 You either need a parameterless constructor on your DbContext class (which you essentially can't do), or implement IDbContextFactory . 您需要在DbContext类(您基本上不能这样做)上使用无参数构造函数,或者实现IDbContextFactory

The method I used was to create an implementation of IDbContextFactory and return an instance of my DbContext from the Create method, like so: 我使用的方法是创建IDbContextFactory的实现并从Create方法返回我的DbContext的实例,如下所示:

public class ApplicationDbContext : DbContext<ApplicationDbContext>
{
    // DbContext code as normal
}

public class ApplicationDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

Note you have to create a DbContextOptionsBuilder to pass through to the DbContext , however since migrations do not actually hit the database and just read your entity classes I don't believe you need to actually configure any options, except for possibly the Database Provider as I've run into issues with migration code generated that worked for one provider but not another. 请注意,您必须创建一个DbContextOptionsBuilder以传递给DbContext ,但是由于迁移实际上并未访问数据库而只是读取您的实体类,我认为您不需要实际配置任何选项,除了可能的数据库提供程序,因为我已经遇到了生成的迁移代码问题,这些代码适用于一个提供商而不是另一个提供商

Now when you run the migration, EF should automatically find the Factory class and use that to create a new DbContext. 现在,当您运行迁移时,EF应自动查找Factory类并使用它来创建新的DbContext。

For whatever it's worth, I encountered this error in ASP.Net Core 2.1/EF because of a syntax error in my "appsettings.json": 无论它值多少,我在ASP.Net Core 2.1 / EF中遇到此错误,因为我的“appsettings.json”中存在语法错误:

  • BAD APPSETTINGS: 不好的选择:

     { "ConnectionStrings": { "DefaultConnection": "\\"Server=(LocalDb)\\\\\\\\MSSQLLocalDB;Database=demoangulardatabase;", "Trusted_Connection=True;MultipleActiveResultSets=true\\"" }, ... 
  • Failed "migrate" command: 失败的“迁移”命令:

     dotnet ef migrations add initialMigration -c ApplicationDbContext -v ... Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. ---> System.MissingMethodException: No parameterless constructor defined for this object. ... 
  • Corrected appsettings.json: 更正了appsettings.json:

     "ConnectionStrings": { "DefaultConnection": "Server=(LocalDb)\\\\MSSQLLocalDB;Database=demoangulardatabase;Trusted_Connection=True;MultipleActiveResultSets=true" }, 

After fixing the corrupted appsettings.json, dotnet ef migrations add initialMigration -c ApplicationDbContext -v ran perfectly. 修复损坏的appsettings.json后, dotnet ef migrations add initialMigration -c ApplicationDbContext -v完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架核心:没有为此 dbcontext 定义无参数构造函数 - Entity Framework Core: No parameterless constructor defined for this dbcontext 带有 WPF Core 的 Entity Framework Core - 无法添加迁移 - Entity Framework Core with WPF Core - Cannot add migrations 实体框架核心 - 迁移 - 没有为此对象定义无参数构造函数 - Entity Framework Core - Migration - No Parameterless Constructor Defined for this Object 无参数构造函数中的初始化(实体框架) - Initialization in a Parameterless Constructor (Entity Framework) 实体框架-实体查找不抛出无参数构造函数 - Entity Framework - Entity Find throwing no parameterless constructor Entity Framework Core 和 EF6 - dotnet ef 迁移添加 InitialCreate - 值不能为空。 (参数“连接字符串”) - Entity Framework Core and EF6 - dotnet ef migrations add InitialCreate - Value cannot be null. (Parameter 'connectionString') LINQ / Entity框架:无参数构造函数问题 - LINQ/Entity framework: parameterless constructor issue 测试实体框架核心迁移 - Testing Entity Framework Core Migrations 使用迁移与实体框架核心 - Using Migrations with Entity Framework Core 为什么我必须为Code First / Entity Framework提供无参数构造函数 - Why must I have a parameterless constructor for Code First / Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM