0

I have a DB and I want to add a separate class library for entity framework to access my data but when I run scaffold command it makes a DB model in my web application.

6
  • what command are you running? Commented Nov 13, 2018 at 7:13
  • Scaffold-DbContext "Server=xxx;Database=xxxx;user id=xxx;password=xxx" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DbContext. A folder is present in my class library name DbContext Commented Nov 13, 2018 at 7:17
  • My Project Heirarcy is like this ABCProject ABCWebProject ABC.Data- Class library(Want to setup EntityFrameworkDatabase First in this Class Library) Commented Nov 13, 2018 at 7:25
  • Did you chose your data project as default project in package manager console ? Commented Nov 13, 2018 at 7:37
  • No i dont know about that? Please tell Commented Nov 13, 2018 at 7:43

2 Answers 2

1

You should select your Data project as default project in package manager console.

enter image description here

Be careful, you have ef core references in data.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0-preview3-35497" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0-preview3-35497" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0-preview3-35497" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0-preview3-35497"/>
    <PackageReference Include="System.Data.Common" Version="4.2.1" />
  </ItemGroup>

</Project>

I realized that there are some errors in ClassLibrary projects. You can take a look at them:

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#targeting-class-library-projects-is-not-supported

https://github.com/dotnet/cli/issues/8735

https://github.com/aspnet/EntityFrameworkCore/issues/10298

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @ibrahimozgon i will run and test it.
0

@ibrahimozgon's answer is right and helped me. However, I encountered a few errors in the DbContext class on the way which he didn't mention how to solve:

'EntityTypeBuilder' does not contain a definition for 'ToTable' and no accessible extension method 'ToTable' accepting a first argument of type 'EntityTypeBuilder' could be found (are you missing a using directive or an assembly reference?

and

'KeyBuilder' does not contain a definition for 'ForSqlServerIsClustered' and no accessible extension method 'ForSqlServerIsClustered' accepting a first argument of type 'KeyBuilder' could be found (are you missing a using directive or an assembly reference?

To resolve these errors in DbContext class, open up package manager console again and select the default project to be the class library. Enter these commands one by one:

- Install package:  Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.4  (or whichever latest version)
- Install package:  Install-Package Microsoft.EntityFrameworkCore.Relational -Version 2.2.4  (or whichever latest version)
- If errors persist try: Install-Package or Update-Package  Microsoft.EntityFrameworkCore.Tools  (or whichever latest version)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.