Entity Framework Core 1.0 : Reverse Engineering the Model (Database First Development)

This article uses Entity Framework Core 1.0.0

Database First Microsoft docs here

and here are the steps to build out your model from an existing database (aka. reverse engineer).

Create a new ASP.NET Web Application

c1.PNG

 

Open up your Package Manager Console

  1. Open Tools > NuGet Package Manager > Package Manager Console

    After we type in a command, the packages will automatically install in the background.  While the install is in progress you will see “(Restoring…)” next to the projects References folder.

  2. Type these 3 commands in the Package Manager Console: Install-Package Microsoft.EntityFrameworkCore.SqlServer –Pre
    Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
    Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design –Pre

Your Package Manager Console should look something like this:

pmc.PNG

Open up your project.json file

  1. Add “Microsoft.EntityFramworkCore.Tools” to the “tools” section. It should end up looking something like this:
"tools": {
  "Microsoft.EntityFrameworkCore.Tools": {
    "version": "1.0.0-preview1-final",
    "imports": [
      "portable-net45+win8+dnxcore50",
      "portable-net45+win8"
    ]
  },
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
    "version": "1.0.0-preview1-final",
    "imports": "portable-net45+win8+dnxcore50"
  }
},

Run Auto Scaffold Command in Package Manager Console  (First Option)

  1. First, create a folder in your project named “Models”
  2. Second,  run this command in the Package Manager Console with your server and db info (note: single quotes around connection string, very unusual)
Scaffold-DbContext "'Server=SERVERNAME;Database=DATABASENAME;Trusted_Connection=True;'" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

if connecting to a secure db, use something like this (note: no single quotes needed here! effin crazy!)

Scaffold-DbContext "Server=SERVERNAME;Database=DATABASENAME;user id=USERNAME;password=USERPASSWORD;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

After successfully running the above command, your project should now look something like this:

models.PNG

Run Auto Scaffold Command in Command Prompt  (Second Option)

Another way to run these commands is through the Command Prompt.  To run the scaffold command, open up your command prompt and navigate to the projects root folder.  Then run a command that looks like this:

dotnet ef dbcontext scaffold "Server=SERVERNAME;Database=DATABASENAME;user id=USERNAME;password=USERPASSWORD;" Microsoft.EntityFrameworkCore.<wbr />SqlServer -o Models  --context MyDBDataContext --verbose

Thats it! This is suppose to become easier with the new version of Visual Studio.  Also, if your wondering how to get your SQL Server Views into your data context, check out this post.

 

Advertisements

11 thoughts on “Entity Framework Core 1.0 : Reverse Engineering the Model (Database First Development)

  1. Brent June 1, 2016 / 10:51 pm

    I have a database from an old project, I tried moving it to EF7 and because it had the identity tables in it, the project was a mess with application context problems.

    Have you tried that?

    Like

    • jesse June 2, 2016 / 3:45 pm

      Is this correct – Your trying reverse engineer a model in your project based on your ASP_Identity Database?
      I have not built a model of my ASP_Identity DB so I am not sure what type of issues you are dealing with, but I have successfully connected my ASP_Identity DB to my app through a connection string in the appsettings.json file. Why do you need a code representation of the Identity DB?
      To use MS Identity in your project you’re suppose to configure it in Startup.cs and then connect to the DB in appsettings.json. That appsettings.json connection string will look something like this:
      “ConnectionStrings”: {
      “DefaultConnection”: “Server=XXXXX;Database=ASP_Identity_DB;user id=XXXXXXX;password=XXXXXXXX;MultipleActiveResultSets=true”
      }

      Like

      • brentwoodard7 June 2, 2016 / 7:09 pm

        Yeah I had a webforms .NET 4.0 project using Identity and a SQL Database. I was looking to make a new Core MVC project using EF7. When I reverse engineered the database it brought all the identity tables with it, so then I had three Database Contexts and identity stopped working.

        Like

      • jesse June 3, 2016 / 12:16 am

        It seems odd that it would bring in 3 databases. In the scaffold command you are specifically setting the DB name so it should only get that one DB. Your having an issue that I have not seen before.
        As far as your Identity DB. I would recommend you create a new ASP.Net Core Web Application. When creating the project, select the Web App Template and set Authentication to “Individual User Accounts”. If you do this it will create an example app that is set up to authenticate for you. To connect to your Identity DB all you need to do is open up appsettings.json and paste in your connection string.
        Just like that you have an app authenticating against your existing Identity DB

        Like

  2. Mike Henderson June 3, 2016 / 12:52 pm

    I ran into a gotcha involving SQL Azure. The scaffold command will look for any Sequences you have created. There are a couple of SQL Azure versions, and only the latest supports Sequences. I kept getting errors running the scaffold command until I upgraded the SQL Azure to a version that support Sequences.

    Like

    • jesse June 3, 2016 / 2:20 pm

      Thanks for the heads up. I have not tried running this on an Azure DB yet. I just checked out your issue here: https://github.com/aspnet/EntityFramework/issues/5553

      Is it correct to say that your SQL Azure Version must be version 12 or higher in order to run the scaffold command? I can add that to my post above.

      Like

  3. Thanh Diep June 3, 2016 / 6:02 pm

    Hi Jesse,

    1. How do you reverse engineer into a class library project (acting as a data access layer DAL)?
    2. How do you pass the connection string to the DbContext class in the DAL?

    Thanks,
    Thanh

    Like

  4. Rian October 20, 2016 / 6:04 am

    how about view in sql server. Can EF Core do reverse engineer view in our DB into class in application?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s