Database First Error Entity Framework .NET Core

asp.net-core c# entity-framework-core

Question

I am converting an old Class Library to a Class Library Package. This Class Library is the DAL Layer of an old project - which we will be upgrading to latest .NET.

In the old Class Library we are using edmx files for Database first. In the new .NET the support for edmx has stopped, but we still need to generate the Model from the Database - Because the database is already in place.

For this I am following the Steps provided in this link: https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

So what I have done is created a new Class Library Package:

enter image description here

And the project.json is this:

{
  "version": "1.0.0-*",
  "description": "xPT.DAL Class Library",
  "authors": [ "Dawood" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "net46": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1",
        "System.Collections": "4.0.11",
        "System.Linq": "4.1.0",
        "System.Runtime": "4.1.0",
        "System.Threading": "4.0.11"
      }
    }
  },
  "dependencies": {

  }
}

Now when I install: Microsoft.EntityFrameworkCore.SqlServer, as mentioned in the Link above, I get this error:

Using this command : Install-Package Microsoft.EntityFrameworkCore.SqlServer

enter image description here

The Error List:

enter image description here

It says here:

The dependency Microsoft.Extensions.Caching.Abstractions 1.0.0 in project xPT.DAL does not support framework .NETFramework,Version=v4.6

But according to the documentation, this library should be supported for all above 4.5.1: https://docs.efproject.net/en/latest/providers/sql-server/index.html#supported-platforms

What am I doing wrong?

1
0
2/4/2017 3:41:16 PM

Accepted Answer

Okay it took me about a day to figure it out so here I am posting the steps I followed to get my Database First working in a Class Project (.NET Core), with a .NET Core Web App.

Step 1 - Install .NET Core

Make Sure you are using .NET Core not DNX (Hint: You should be able to see the .NET Core option when creating a New Project) - If NOT Download from Here

If you have problems installing .NET Core (Error is something like Visual Studio 2015 Update 3 not installed correctly) - You can run the installing using the command: [DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1] -- Which will prevent the installation performing the Visual Studio Check Github Issue

enter image description here

Step 2 - Create The Projects

Create a new ASP.NET Core Web Application --> Then Select Web Application in the next screen

enter image description here

Add a Class Library (.NET Core) Project

enter image description here

Step 3 - Installing EF Packages

Open your project.json file of Class Library, and paste the following, then Save the file:

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "NETStandard.Library": "1.6.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net46": {
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    }
  }
}

This should restore the packages under References

enter image description here

---------------- OR

You can install them using Nuget Package Manager by running the following commands in the Package Manager Console

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

Note: Install one Package at a time - if you get an error after installing

Microsoft.EntityFrameworkCore.Tools

Then change the content of your project.json frameworks section to this:

  "frameworks": {
    "net46": {
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    }
  }

Step 4 - Creating the Database Model

Now to generate the Database run the following command in the Package Manager Console (DON'T forget to Change the connection string to your Database)

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer

This will give you the Error about Startup Project:

enter image description here

For this you have to add the same references you added to Class Library to the .NET Web App

So open your project.json for the Web App,

Under dependencies, add:

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

and under tools add:

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

After making the changes Save the file.

This is what my project.json looks like

enter image description here

Then again run the command in Package Manager Console against the class library:

If you haven't already added the reference of your Class Library to the Web App, you will get this error:

enter image description here

to solve this add reference of your class Library to your Web App:

enter image description here

Finally

Run the Command again - in the Package Manager Console:

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

This should create the Entities under Models Folder, in the class library

enter image description here

2
8/11/2016 9:51:33 AM

Popular Answer

Update:
Looks like you're still using DNX. Please upgrade to .NET Core (along with .NET CLI) as DNX is no longer supported. You can download .NET Core here.


net46 is a Target Framework Moniker (TMF) for desktop .NET framework 4.6. You don't need to reference packages such as Microsoft.CSharp and System.Collections on that framework since they are part of the full .NET framework by default. You only need those when targeting a .NET Standard version such as netstandard1.6.

Remove all the depdendencies you listed under net46:

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
  },

  "frameworks": {
    "net46": {}
  }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow