尝试在类库项目中首先添加EF数据库,但它失败并发出错误
启动项目' Api.DB.csproj'目标框架' .NETStandard'。 没有与此框架和项目关联的运行时 定位它不能直接执行。使用实体框架 核心.NET命令行工具与此项目,添加可执行文件 针对引用此内容的.NET Core或.NET Framework项目 项目,并使用--startup-project将其设置为启动项目; 或者,更新此项目以跨目标.NET Core或.NET Framework。
步骤
1.克里特岛新解决方案
dotnet new sln --name TestApi
dotnet new webapi --name api
dotnet new classlib --name Api.DB
dotnet sln" TestApi.sln"添加" api / api.csproj"
dotnet sln" TestApi.sln"添加" Api.DB / Api.DB.csproj"
PS F:\ POC \ Projects_POC \ EF_Core2_Delete3 \ api> dotnet添加引用../ Api.DB / Api.DB.csproj
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1"/>
dotnet ef dbcontext Scaffold &#34;服务器= GaneshSqlServevr;数据库=汽车; Trusted_Connection =真;&#34; Microsoft.EntityFrameworkCore.SqlServer -o Models
当前的dotnet版本是2.1.100
这里是Api.DB.csproj文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
</Project>
答案 0 :(得分:1)
运行时框架版本适用于我,这是csproj文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
</Project>
答案 1 :(得分:0)
您必须在.csproj文件中添加以下行,才能在.net核心类库项目中添加EF核心。
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>