c# - 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 中工作?
解决方案
你需要实施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);
}
}
推荐阅读
- core-data - 核心数据轻量级迁移 - 添加版本时 xcode 崩溃
- java - 如何找到两棵句法树的公共树
- asp.net-mvc - 出现错误“没有类型为 'IEnumerable 的 ViewData 项
'这有关键的'状态'。” - c# - 将递归树映射到带有条件的平面列表
- android - 在android中运行api调用时本地主机IP地址不起作用
- sql - 如何使用合并语句在不更改项目顺序的情况下将批量数据插入表中
- mongodb - 如何配置在更新事件上发送的 debezium 字段(mongo 连接器)
- android - Android - 将数据从 Realm 迁移到 Room
- php - 在codeigniter中更新/替换没有表格的数据
- jenkins - Jenkins Xvfb 插件 - 在构建中获取显示名称偏移量