Trying to add EF database first in Class library project but it fails and gives an error
Startup project 'Api.DB.csproj' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core .NET Command Line Tools with this project, add an executable project targeting .NET Core or .NET Framework that references this project, and set it as the startup project using --startup-project; or, update this project to cross-target .NET Core or .NET Framework.
Steps
1. Crete new solution
dotnet new sln --name TestApi
- Create Web Api project
dotnet new webapi --name api
- Create new class library project
dotnet new classlib --name Api.DB
- Add reference of this two project in solution
dotnet sln "TestApi.sln" add "api/api.csproj"
dotnet sln "TestApi.sln" add "Api.DB/Api.DB.csproj"
- Add class library project reference in web api project
PS F:\POC\Projects_POC\EF_Core2_Delete3\api> dotnet add reference ../Api.DB/Api.DB.csproj
- Add packages in Class library project
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
- Add Microsoft.EntityFrameworkCore.Tools.DotNet in csproj file
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1"/>
- And finally, run scaffold command
dotnet ef dbcontext Scaffold "Server=GaneshSqlServevr;Database=Car;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
The current dotnet version is 2.1.100
and here Api.DB.csproj file
<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>