实体框架7数据库优先配置(MVC 6) [英] Entity Framework 7 Database First configuration (MVC 6)

查看:59
本文介绍了实体框架7数据库优先配置(MVC 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过长期的不懈努力,终于弄清楚了如何使用MVC 6首先使用EF7数据库.方法是这样的:

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:

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"
},

然后从cmd运行命令:

Then running the command from cmd:

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

我正面临着3个问题.在我的Impl项目上.

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

  1. 我有一个专用于Data的文件夹.我的数据文件夹应包含所有与数据库相关的内容,例如DbContext,DbClasses.但是,Scaffold在根目录下创建这些文件.如何指定要创建这些文件的文件夹?

  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?

在我的工作场所,我可以有一个包含50个表的数据库.但是,如果我只需要访问2怎么办?我不想为我的项目添加所有50个表.如何指定我想要的表?

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?

在我的数据库上,我可以有一个名为"Users"的表,因为它是包含用户的表.但是,脚手架应该将一个类创建为"User",因为在成为List之前,它是单个用户.如何指定要创建的表的名称?

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?

谢谢大家的帮助.

推荐答案

尝试使用

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

以上所有参数应该在同一行中,但是我将长行换行以使其更容易阅读.您可以查看源代码,以查看哪些选项支持RC1中的scaffold命令.

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.

请仔细复制并粘贴ConnectionString中的ConnectionString,因为您可能在ConnectionString中包含Server=Server\\InstanceName;,但是dnx ef dbcontext scaffold当前仅接受Server=Server\InstanceName;,并且会出现 System.InvalidOperationException错误:直接从appsettings.jsonConnectionString复制的Server=Server\\InstanceName;的用法.

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.

其他重要参数是-p | --targetProject,如果您在类库中使用存储库,则该参数很重要.如果您在主项目中定义ef命令,并在主项目的目录中启动dnx ef dbcontext scaffold,但是使用-p引用类库项目.

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.

最后一句话.有时,需要从数据库中对表格的子集进行设置.在命令行帮助中尚不清楚,但是一个人可以多次指定-t(-table)参数.参见 EF7 Wiki中的注释.因此,如果您只想导入两个表dbo.Usersdbo.Posts(Posts是否具有Users的外键),则可以使用以下语法

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

更新:在ASP.NET Core RC2和更高版本中,应该使用dotnet ef dbcontext scaffold而不是dnx ef dbcontext scaffold.

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

这篇关于实体框架7数据库优先配置(MVC 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆