1

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

  1. Create Web Api project

dotnet new webapi --name api

  1. Create new class library project

dotnet new classlib --name Api.DB

  1. 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"

  1. 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

  1. 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

  1. Add Microsoft.EntityFrameworkCore.Tools.DotNet in csproj file
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1"/>  
  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>
Ganesh Jangam
  • 97
  • 1
  • 4
  • 15
  • Would changing `netstandard2.0` to `netcoreapp2.0` fix it? – Wubbler Mar 11 '18 at 09:14
  • hello Wubbler it gives me The specified framework version '2.0' could not be parsed The specified framework 'Microsoft.NETCore.App', version '2.0' was not found. - Check application dependencies and target a framework version installed at: \ - Alternatively, install the framework version '2.0'. – Ganesh Jangam Mar 11 '18 at 09:43
  • 2
    Possible duplicate of [Trying to set-up Entity Framework core in .Net Standard project](https://stackoverflow.com/q/48673987) – NightOwl888 Mar 11 '18 at 10:05
  • Hello NightOwl888, Its work with Visual Studio 2017 but not in visual studio code – Ganesh Jangam Mar 11 '18 at 13:51
  • my current installed version mention as follows 2.0.5 this works for me Thank you NightOwl888 and Wubbler – Ganesh Jangam Mar 11 '18 at 14:19

2 Answers2

1

Runtime Framework Version works for me, Here is csproj file

<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>
Ganesh Jangam
  • 97
  • 1
  • 4
  • 15
0

You have to add the following line in your .csproj file to add EF core in a .net core class library project.

<PropertyGroup>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
Aditi
  • 23
  • 6