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

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

问题描述

经过长期不懈的努力,终于弄清楚了如何使用 MVC 6 使用 EF7 Database first 方法.事情是这样的:

App.Impl -> project.json:

框架":{net451":{},dnx451":{}},依赖":{"EntityFramework.Commands": "7.0.0-rc1-final","EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",EntityFramework.MicrosoftSqlServer.Design":7.0.0-rc1-final"},

然后从 cmd 运行命令:

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

我面临的三个问题.在我的 Impl 项目中.

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

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

  3. 在我的数据库中,我可以有一个名为Users"的表,因为它是一个包含用户的表.但是,scaffold 应该将类创建为User",因为它在变为 List 之前是单个用户.如何指定正在创建的表的名称?

感谢大家的帮助.

解决方案

尝试使用

dnx ef dbcontext 脚手架服务器=服务器实例名称;数据库=db;Trusted_Connection=True;"EntityFramework.MicrosoftSqlServer--dataAnnotations--outputDir 数据--详细--table dbo.Users

以上所有参数都应该在同一行,但我把长行换了,以便阅读.您可以查看 源代码查看哪些选项支持RC1中的scaffold命令.

小心地复制和粘贴 appsettings.json 中的 ConnectionString,因为您可能在 Server=Server\InstanceName; 中有 Server=Server\InstanceName;>ConnectionString,但 dnx ef dbcontext scaffold 目前只接受 Server=ServerInstanceName;,你会得到 System.InvalidOperationExceptionServer=Server\InstanceName; 的使用直接从 appsettings.jsonConnectionString 复制过来的 code> 错误.

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

最后一句话.有时需要从数据库中构建 子集 表.命令行帮助不是很清楚,但是可以多次指定-t(-table)参数.请参阅 EF7 wiki 中的注释.因此,如果您只想导入 dbo.Usersdbo.Posts 两个表(Posts 是否具有 Users) 那么你可以使用下面的语法

dnx ef dbcontext 脚手架服务器=服务器实例名称;数据库=db;Trusted_Connection=True;"EntityFramework.MicrosoftSqlServer-一个-o 模型-v-t dbo.用户-t dbo.帖子

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

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.

解决方案

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.

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

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