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.
-
what command are you running?ibrahimozgon– ibrahimozgon2018-11-13 07:13:05 +00:00Commented 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 DbContextAnkit Mittal Learner– Ankit Mittal Learner2018-11-13 07:17:21 +00:00Commented 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)Ankit Mittal Learner– Ankit Mittal Learner2018-11-13 07:25:00 +00:00Commented Nov 13, 2018 at 7:25
-
Did you chose your data project as default project in package manager console ?ibrahimozgon– ibrahimozgon2018-11-13 07:37:55 +00:00Commented Nov 13, 2018 at 7:37
-
No i dont know about that? Please tellAnkit Mittal Learner– Ankit Mittal Learner2018-11-13 07:43:14 +00:00Commented Nov 13, 2018 at 7:43
2 Answers
You should select your Data project as default project in package manager console.
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:
1 Comment
@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)
