12

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?

Community
  • 1
  • 1
awh112
  • 1,466
  • 4
  • 22
  • 34
  • When you say you have had "little luck" with those commands, what was the problem exactly? – DavidG Oct 08 '15 at 14:14
  • @DavidG when I run the command in the prompt, I just get no result. No error, but also no success. My context file remains empty and I don't see another one added to the filesystem. – awh112 Oct 08 '15 at 14:24
  • Re: The Tags: Surely this is not about Asp.Net Core, nor EF Core. This was from that time when there was a .NET 5 that pre-dated "core" and was intended as the successor to .NET Framework 4.x. How can we get these "core" tags removed? And the "Entity Framework 7" in the title ... even more confusing. – Ian W Jan 27 '23 at 23:04

4 Answers4

12

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.

adrianosepe
  • 55
  • 1
  • 6
Nick De Beer
  • 5,232
  • 6
  • 35
  • 50
  • Followed the same steps except I had to install all packages from command line with "dnu", thanks! – LeLong37 Oct 25 '15 at 09:34
  • your DAL layer is a project of which type? most project types don't support project.json – tkit Jan 29 '16 at 01:09
  • 1
    @pootzko I used a Class Library (Package) in framework 4.6. Referenced that in my BL which is also a class library, and referenced the BL in my ASP.NET 5 Web Application. Splitting worked for me because of the complexity of my project, but you can do all of it in one Web Application project actually. – Nick De Beer Jan 29 '16 at 01:17
  • 1
    Nah, I want to split it. What you just explained is exactly what I want. Thanks for the prompt answer. :) – tkit Jan 29 '16 at 01:19
3

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

dnx ef dbcontext scaffold connectionstring provider -t dbo.tab1 -t dbo.tab2
Kirsten
  • 492
  • 4
  • 11
0

For EF 7, you no longer need to initialise with base, it acually results in a compiler error. what you need to look as it the Microsoft EF7 starting docs.

You want to pay attention to the new

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Visual Studio 2015 | Use the LocalDb 12 instance created by Visual Studio
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

        // Visual Studio 2013 | Use the LocalDb 11 instance created by Visual Studio
        // optionsBuilder.UseSqlServer(@"Server=(localdb)\v11.0;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

        // Visual Studio 2012 | Use the SQL Express instance created by Visual Studio
        // optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
    }
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
Phillip Morton
  • 244
  • 2
  • 13
-4

I'd try code-first from an existing database. You can create your context class to look something like this:

public class MyDbContext : DbContext {

        public MyDbContext()
            : base("connectionString") {
            Database.SetInitializer<MyDbContext>(null);
        }

        public virtual DbSet<Table1> Table1s { get; set; }

        public virtual DbSet<Table2> Table2s { get; set; }

        ...
}

You can include DbSets for only those tables you need. You create the corresponding models you need (Table1, Table2, etc.) and scaffold controllers and views from those models.

sschimmel
  • 196
  • 6
  • Good luck doing that if you need to model 100 tables! – DavidG Oct 08 '15 at 14:14
  • Thanks for the suggestion! The problem with this seems to be that the Database you are referencing above is System.Data, while I'm using Microsoft.Data and the DatabaseFacade object exposed off of the base context doesn't have the 'SetInitializer' method. – awh112 Oct 08 '15 at 14:14
  • @DavidG Luckily I only need to model ~5 tables out of the many that are there! – awh112 Oct 08 '15 at 14:18
  • @DavidG: Indeed... but since he only needed a few, it should be pretty easy. – sschimmel Oct 08 '15 at 14:19
  • @awh112: You're not using System.Data.Entity? – sschimmel Oct 08 '15 at 14:20
  • @sschimmel No, when I used System.Data it didn't work in my Startup.cs where I'm doing `services.AddEntityFramework().AddDbContext(options => options.UseSqlServer(slConnection));` – awh112 Oct 08 '15 at 14:23