Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

asp.net core - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to use

dnx ef dbcontext scaffold 
    "Server=ServerInstanceName;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=ServerInstanceName; 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=ServerInstanceName;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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...