Entity Framework 7 DbContext scaffold

泄露秘密 提交于 2019-12-05 00:35:46
Nick De Beer

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.

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!