Entity Framework 7 Database First configuration (MVC 6)

Entity Framework 7 Database First configuration (MVC 6)

After a long constant struggle, finally figured out how to use EF7 Database first approach using MVC 6. This is how it goes:

App.Impl -> project.json:

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

Then running the command from cmd:

dnx ef dbcontext scaffold "Server=ip,port;Database=db;User Id=user;Password=pass;" EntityFramework.MicrosoftSqlServer

3 problems I am facing with this. On my Impl project.

  1. I have a folder dedicated to Data. My Data folder should contain all Database related things such as DbContext, DbClasses. However, Scaffold creates these files at the root. How can I specify the folder I want these files created?

  2. At my work place, I could have a database with 50 tables. However, what if I only need access to 2? I don't want to add all 50 tables for my project. How can I specify which tables I want?

  3. On my Database, I could have a table named "Users" because it is a table that contain users. However, scaffold should be creating a class as "User", since it is a single user until it becomes List. How can I specify the name of the table being created?

Thank you all for the help.

 

回答1

Try to use

dnx ef dbcontext scaffold 
    "Server=Server\InstanceName;Database=db;Trusted_Connection=True;"
    EntityFramework.MicrosoftSqlServer 
    --dataAnnotations
    --outputDir Data
    --verbose
    --table dbo.Users

All the above parameters should be in the same line, but I wrapped the long line to read it more easy. You can look at the source code to see which options supports scaffold command in RC1.

Be carefully in copy and paste the ConnectionString from appsettings.json because you could have Server=Server\\InstanceName; in ConnectionString, but dnx ef dbcontext scaffold accept currently only Server=Server\InstanceName; and you will get System.InvalidOperationException error on the usage of Server=Server\\InstanceName; directly copied from the ConnectionString of appsettings.json.

Additional important parameter is -p | --targetProject, which is important if you use repository in the class library. In the case you defines ef command in the main project, and you start dnx ef dbcontext scaffold in the directory of the main project, but you use -p to reference the class library project.

The last remark. Sometimes one need to scaffold subset of tables from the database. It's not full clear from the command line help, but one can specify -t (-table) parameter multiple times. See the note in the EF7 wiki. Thus if you want to import only two tables dbo.Users and dbo.Posts (whether Posts have foreign key to Users) then you can use the following syntax

dnx ef dbcontext scaffold 
    "Server=Server\InstanceName;Database=db;Trusted_Connection=True;"
    EntityFramework.MicrosoftSqlServer 
    -a
    -o Models
    -v
    -t dbo.Users
    -t dbo.Posts

UPDATED: One should use dotnet ef dbcontext scaffold instead of dnx ef dbcontext scaffold in ASP.NET Core RC2 and later.

 

注意,这里的-o Models是设置输出目录为Models,会自动创建目录

 

Can we Scaffold DbContext from selected tables of an existing database [duplicate]

 

This question already has answers here:

As in previous versions of Entity Framework, is it possible in Entity Framework Core to reverse engineer only the selected tables of an existing database to create model classes out of them. This official ASP.NET site reverse engineers the entire database. In past, as shown in this ASP.NET tutorial, using old EF you could reverse engineer only the selected tables/Views if you chose to.

 

回答1

One can solve the problem by usage of dotnet ef dbcontext scaffold command with multiple -t (--table) parameters. It allows to specify all the tables, which needed by imported (scaffolded). The feature is described initially here.

It is possible to specify the exact tables in a schema to use when scaffolding database and to omit the rest. The command-line examples that follow show the parameters needed for filtering tables.

.NET Core CLI:

dotnet ef dbcontext scaffold
          "server=localhost;port=3306;user=root;password=mypass;database=sakila" 
         MySql.Data.EntityFrameworkCore -o sakila
         -t actor -t film -t film_actor -t language -f  

Package Manager Console in Visual Studio:

Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila"
     MySql.Data.EntityFrameworkCore -OutputDir Sakila
     -Tables actor,film,film_actor,language -f   

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-10-20 Autofac Exception Summary Autofac异常汇总
2021-10-20 Cross-Origin Resource Sharing
2021-10-20 Autofac not working with background tasks
2021-10-20 Why exactly is void async bad?
2018-10-20 启动vmware中的虚拟机的时候,提示Failed to lock the file
2016-10-20 筛选或者删除数据表中重复的数据
点击右上角即可分享
微信分享提示