C# 尝试通过共享和迁移数据库上下文时出错
我开始学习asp.net core 3,目前我有一个问题。我试图通过.net核心dll像以前一样共享我的db上下文,但我有一个错误 我在课堂上声明了以下内容C# 尝试通过共享和迁移数据库上下文时出错,c#,asp.net-core,C#,Asp.net Core,我开始学习asp.net core 3,目前我有一个问题。我试图通过.net核心dll像以前一样共享我的db上下文,但我有一个错误 我在课堂上声明了以下内容 // This method gets called by the runtime. Use this method to add services to e container. public void ConfigureServices(IServiceCollection services) { services.AddCon
// This method gets called by the runtime. Use this method to add services to e container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DbContext_Model>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppliedContext")));
}
//此方法由运行时调用。使用此方法向e容器添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“AppliedContext”));
}
我的核心dll项目解决方案中的db上下文
public class DbContext_Model: DbContext
{
public DbContext_Model(DbContextOptions<DbContext_Model> options)
: base(options)
{ }
public DbSet<Customer> Customer { get; set; }
public DbSet<Policys> Policys { get; set; }
}
公共类DbContext\u模型:DbContext
{
公共DbContext_模型(DbContextOptions)
:基本(选项)
{ }
公共数据库集客户{get;set;}
公共数据库集策略{get;set;}
}
我已在下面声明了我的连接字符串,密码已被屏蔽
“连接字符串”:{
“AppliedContext”:“服务器=桌面\MSSQLSERVER2017;数据库=系统;用户”
Id=sa;密码=xxxxx;MultipleActiveResultSets=true”
但是当我添加Migration InitalCreate时,我得到了这个错误
无法创建类型为“DbContext\u Model”的对象。对于
设计时支持的不同模式,请参阅
是否不再允许在.net核心库和网站之间共享上下文
编辑2
请参阅屏幕截图,显示我正在选择正确的项目。
编辑3
这似乎导致了另一个错误,如下所示。因此,我在json工具中添加了nugets,但仍然会出现此错误
dotnet ef迁移添加InitialCreate System.MissingMethodException:
找不到方法:“System.Text.Json.JsonDocument”
System.Text.Json.JsonDocument.Parse(System.IO.Stream,
System.Text.Json.JsonReaderOptions)').at
Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute()位于
Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.c__DisplayClass0_0.b_0()
在
Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(字符串[])
args),位于Microsoft.EntityFrameworkCore.Tools.Program.Main(字符串[])
找不到args)方法:“System.Text.Json.JsonDocument”
System.Text.Json.JsonDocument.Parse(System.IO.Stream,
System.Text.Json.JsonReaderOptions)“.PM>dotnet ef迁移添加
初始创建
试着这样做:
options.UseSqlServer(
Configuration.GetConnectionString("AppliedContext"),
options => options.MigrationsAssembly(<DB_PROJECT>)
从您的Edit3中的屏幕截图,您需要将EF Core相关软件包更新到3.0版。在管理NutGet软件包中,选中搜索框旁边的包括预发布,并将其更新到最新版本,如下所示:
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0-preview8.19405.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview8.19405.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview8.19405.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
netcoreapp3.0
全部的
运行时;构建;本机;内容文件;分析器;构建可传递
全部的
运行时;构建;本机;内容文件;分析器;构建可传递
注意:添加迁移时,请将web应用程序设置为启动项目,并将类库设置为要在其中生成迁移文件夹的默认项目
参考资料:您的Edit3的屏幕截图似乎表明您的EF工具版本已过时。您可以通过执行以下命令进行检查:
dotnet ef --version
在命令行上。如果它不是preview8
,则安装它:
dotnet tool update dotnet-ef --version 3.0.0-preview8.19405.11
(可选使用--global
开关)
注意:EF preview8在与一起使用时有一个附加的。它应该在preview9中修复(在撰写本文时它还没有发布).您可能试图使用错误的项目创建迁移。您必须使用实际配置上下文的项目。否则,迁移工具将无法构建它。至少如果您希望以这种方式配置它。您好,请查看屏幕截图,我正在选择共享库。好的-我不知道您正在选择使用3.0。尝试以下操作:尝试运行
dotnet工具安装--global dotnet ef--version 3.0.0-*
来更新dotnet ef
工具。
dotnet tool update dotnet-ef --version 3.0.0-preview8.19405.11