Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 7 DbContext scaffold

I am trying to generate a DbContext for an existing database structure using ASP.NET 5 and Entity Framework 7. Not surprisingly, there isn't a lot of documentation surrounding how to do this easily. Additionally, I want to scaffold ONLY the context; there are ~900 tables and I only care about a few of them, I don't need a model class for each one.

I've been using the commands specified here and here with little luck.

So, I guess I have two questions:

  1. Where are the generated context files located? I run the command in the command prompt with no failure, but nothing else happens. I know I'm at least in the right place as I can add the old EF6 model with unsupported properties and it gives me an error that they are not supported.

  2. Is it possible to generate just a context with no corresponding model classes?

like image 835
awh112 Avatar asked Oct 08 '15 13:10

awh112


People also ask

What is Scaffold-DbContext?

Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed using the Scaffold-DbContext command of the EF Core Package Manager Console (PMC) tools or the dotnet ef dbcontext scaffold command of the . NET Command-line Interface (CLI) tools.

Is Scaffold-DbContext command is used in EF Code First approach?

The EF core only supports Code First & Database First approach. In Database First, We use the Scaffold-dbcontext to create the Model from an existing database. This is basically Reverse engineering the existing database. Once we create the entity classes databases first does not work.

Is DbContext Singleton or scoped?

First, DbContext is a lightweight object; it is designed to be used once per business transaction. Making your DbContext a Singleton and reusing it throughout the application can cause other problems, like concurrency and memory leak issues.


2 Answers

I had the same problem with my project generating the context and the models. Here's a few things that I did.

Updates For 1.0 RC1 below

Project.json

  "dependencies": {
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final"
  },

  "commands": {
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }

DNX Command

dnx ef dbcontext scaffold "connectionString" EntityFramework.MicrosoftSqlServer

Original Post Below

Make sure these are added to your project.json file:

"dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta7",
    "EntityFramework.Commands": "7.0.0-beta7",
    "EntityFramework.SqlServer.Design": "7.0.0-beta7"
},
"commands": {
    "ef": "EntityFramework.Commands"
}

Upgrade dnvm and the dnx runtimes as well using dnvm update-self and dnvm upgrade. I ran this in cmd.

Opened cmd.exe in the project location (if you are in windows, navigate to the folder and shift + right-click in the folder and click "Open command window here"). In my case I had a separate project for my Data Access Layer eg.

C:\Projects\Stackoverflow Example\src\StackoverflowExample.DAL\

I then simplay ran:

dnx ef dbcontext scaffold "Data Source=.;Initial Catalog=database;Integrated Security=True" EntityFramework.SqlServer

Make sure your project can build. If there are errors, the commands probably wont work.

It generated all the models as well as the context (with the OnModelCreating() setup of each entity). If you don't need all the models, just delete the ones you are not using.

So to answer you questions:

  1. It creates the models and context in the folder where you ran the dnx ef dbcontext scaffold in.
  2. I cant see any commands that allows you to do this yet. Run dnx ef --help in cmd and look for yourself. dnx ef

I hope this helps.

like image 187
Nick De Beer Avatar answered Sep 19 '22 02:09

Nick De Beer


An option --table (-t) exists for the scaffold command:

dnx ef dbcontext scaffold connectionstring provider -t dbo.tab1 -t dbo.tab2
like image 37
Kirsten Avatar answered Sep 21 '22 02:09

Kirsten