首页 > 解决方案 > EF Core 6 无法完成迁移,无法创建类型的对象

问题描述

但是,我在测试应用程序中使用 ef core 的最新预览版 6 来学习制动变化。我有几件事错了

using System;
using System.Data.Entity;
using Concierge.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
public class ConciergeDBContext : IdentityDbContext<ApplicationUser>
{
    public ConciergeDBContext(Microsoft.EntityFrameworkCore.DbContextOptions<ConciergeDBContext>
    options)
      : base(options)
    {

    }
    public DbSet<Building> Buildings { get; set; }

    protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

出于某种原因,我必须使用 Microsoft.EntityFrameworkCore.DbContextOptions 来限定我的模型构建器,因为如果我没有对我的 DB Set 命令产生歧义,但当我编译它时,这还不是全部。

无法创建“ConciergeDBContext”类型的对象。有关设计时支持的不同模式,请参阅设计时 支持的https://go.microsoft.com/fwlink/?linkid=851728 ,请参阅https://go.microsoft.com/fwlink/?linkid=851728

现在我将我的 web api 项目选择为启动项目并将其配置为

services.AddDbContext<ConciergeDBContext>
(options => 
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")), 
ServiceLifetime.Transient);

那么到底为什么我会收到上述错误。

我正在使用命令

dotnet ef migrations add InitialCreate -p  Concierge.Dal

当我得到上述内容时, outputdir 在 ef 6 世界中似乎也不是一个有效的参数。

我的项目结构。

在此处输入图像描述

我的 Concerige.Dal nugets

<ItemGroup>
 <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-preview.6.21355.2" />
 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.6.21352.1" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.0-preview.6.21352.1" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.6.21352.1">
 <PrivateAssets>all</PrivateAssets>
 <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 </PackageReference>
 <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0-preview.6.21352.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.6.21352.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.6.21352.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

我的上下文存在于 dal 项目中。

在此处输入图像描述

我使用 IdentityDbContext 的原因是在我的用户表上创建自定义字段,所以这可能是问题的一部分,但这曾经在 ef 5 中工作?

标签: c#entity-framework-core

解决方案


你需要实施IDesignTimeDbContextFactory

您还可以通过实现 Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory 接口来告诉工具如何创​​建 DbContext:如果在派生 DbContext 所在的同一项目或应用程序的启动项目中找到实现此接口的类,则工具会绕过创建 DbContext 并使用设计时工厂的其他方法。

将实现放在ConciergeDBContext类下。因此,在迁移时它会被发现并被调用。

public class ConciergeDBContext : IdentityDbContext<ApplicationUser>
{
    public ConciergeDBContext(Microsoft.EntityFrameworkCore.DbContextOptions<ConciergeDBContext>
    options)
      : base(options)
    {

    }
    public DbSet<Building> Buildings { get; set; }

    protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
public class ConciergeDBContextFactory : IDesignTimeDbContextFactory<ConciergeDBContext>
{
    public ConciergeDBContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ConciergeDBContext>();
        optionsBuilder.UseSqlServer(_connection_string);

        return new ConciergeDBContext(optionsBuilder.Options);
    }
}

推荐阅读