-->

Entity Framework 7 DbContext scaffold

2020-08-11 10:20发布

问题:

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?

回答1:

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.



回答2:

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

dnx ef dbcontext scaffold connectionstring provider -t dbo.tab1 -t dbo.tab2


回答3:

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;");
    }


回答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.