迁移错误 - 无法搭建 DirectConstructorBinding 类型的字面量
Migrations Error - cannot scaffold literals of type DirectConstructorBinding
我正在学习 Julie Lerman 的 Pluralsight 课程 -- EntityFramework Core 2: Getting Started
。在本课程中,她有 3 个项目。前两个 Data
和 Domain
基于 .NET Standard 库。第三个项目 Web
是一个 .NET Core Web 应用程序。
我遵循了这个结构。在 Data
中,我添加了一个名为 Client.
的 POCO class
在 Domain
中,我添加了一个 class 名为 TestDbContext ,如下所示:
public class TestDbContext : DbContext
{
public DbSet<Client> Clients { get; set; }
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
}
按照她的例子,我在 Web
项目的 Startup.cs 中执行了以下操作,将提供程序和连接字符串注入 DbContext。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<TestDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("TestConnection"));
});
}
最后,我尝试将迁移添加到此上下文中。我将 Web
项目设置为启动项目。在程序包管理器控制台中,我键入 add-migration initial
.
然后我收到以下错误:The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
在 Julie 的视频中,这一切都对她有效,并且创建了迁移包。然而,对我来说——只是错误。关于可能发生的事情的任何线索?
检查“.csproj”文件中项目的包版本。以前我对 AspNet Core 和 EntityFramework Core 的版本匹配有同样的问题(我猜)。让这些对我来说很好。找到解决方案 Here
现在对我来说。
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0-rc1-final" PrivateAssets="All" />
这对我有用!
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.1.0- preview1-final" />
</ItemGroup>
</Project>
从 nuget 包管理器添加 3 个相同版本的包
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
将插件 Microsoft.EntityFrameworkCore.Design 安装到数据库项目中(其中 dbcontext class 创建项目)
这对我有用:
->打开 WebApp .csproj 文件并进行以下更改:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
现在变成:
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
->将 nugget 包中的最新 Microsoft.AspNetCore.App 安装到 Web 应用程序中
->注释掉这一行:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
->通过注释掉 "app.UseBrowserLink()" 来编辑 StartUp.cs 文件
->重新编译解决方案
->添加迁移:add-migration初始
编码愉快...
我正在学习 Julie Lerman 的 Pluralsight 课程 -- EntityFramework Core 2: Getting Started
。在本课程中,她有 3 个项目。前两个 Data
和 Domain
基于 .NET Standard 库。第三个项目 Web
是一个 .NET Core Web 应用程序。
我遵循了这个结构。在 Data
中,我添加了一个名为 Client.
在 Domain
中,我添加了一个 class 名为 TestDbContext ,如下所示:
public class TestDbContext : DbContext
{
public DbSet<Client> Clients { get; set; }
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
}
按照她的例子,我在 Web
项目的 Startup.cs 中执行了以下操作,将提供程序和连接字符串注入 DbContext。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<TestDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("TestConnection"));
});
}
最后,我尝试将迁移添加到此上下文中。我将 Web
项目设置为启动项目。在程序包管理器控制台中,我键入 add-migration initial
.
然后我收到以下错误:The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
在 Julie 的视频中,这一切都对她有效,并且创建了迁移包。然而,对我来说——只是错误。关于可能发生的事情的任何线索?
检查“.csproj”文件中项目的包版本。以前我对 AspNet Core 和 EntityFramework Core 的版本匹配有同样的问题(我猜)。让这些对我来说很好。找到解决方案 Here
现在对我来说。
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0-rc1-final" PrivateAssets="All" />
这对我有用!
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.1.0- preview1-final" />
</ItemGroup>
</Project>
从 nuget 包管理器添加 3 个相同版本的包
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
将插件 Microsoft.EntityFrameworkCore.Design 安装到数据库项目中(其中 dbcontext class 创建项目)
这对我有用:
->打开 WebApp .csproj 文件并进行以下更改:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
现在变成:
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
->将 nugget 包中的最新 Microsoft.AspNetCore.App 安装到 Web 应用程序中
->注释掉这一行:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
->通过注释掉 "app.UseBrowserLink()" 来编辑 StartUp.cs 文件 ->重新编译解决方案
->添加迁移:add-migration初始
编码愉快...