有没有办法让 EF 5 代码首先迁移使用 ASP.NET MVC 4 中的 sql server 数据库? [英] Is there a way to make EF 5 code first migrations use a sql server database in ASP.NET MVC 4 for everything?

本文介绍了有没有办法让 EF 5 代码首先迁移使用 ASP.NET MVC 4 中的 sql server 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动一个新的 ASP.NET MVC 4 Web 应用程序项目时,我希望它默认使用 SQL Server.默认情况下,当您运行此项目时,它会创建一个 LocalDb 实例并在其中创建以下表:

I would like for it to use SQL Server by default for everything when I start a new ASP.NET MVC 4 Web Application project. By default when you run this project it creates a LocalDb instance and creates the following tables in it:

  • webpages_Membership
  • webpages_OAuthMembership
  • webpages_Roles
  • webpages_UsersInRoles
  • 用户资料

我已经能够使用代码优先迁移将 UserProfile 表放入 SQL 服务器数据库.但是,我无法使用其他 4 个表完成此操作.

I have been able to use a code first migration to put the UserProfile table into a SQL server database. However, I have not been able to accomplish this with the other 4 tables.

显然,我的目标是从单个 SQL Server 数据库开始,并将应用程序的所有表都包含在单个数据库中.

Obviously my goal is to start with a single SQL Server database and have all of the tables for the application contained within the single database.

推荐答案

打开您的 InitializeSimpleMembershipAttribute.cs 文件,这是 WebSecurity 数据库初始化的位置.您需要使用正确的 connectionStringName 对其进行修改.示例:

Open your InitializeSimpleMembershipAttribute.cs file, this is where the WebSecurity database initialization is. You need to modify it with the correct connectionStringName. Example:

改变

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

WebSecurity.InitializeDatabaseConnection("MyRealDBConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

如果您想复制 Web 安全表的结构以与 Code First 一起使用,这里有一篇文章......我目前找不到......给我一些.

IF you want to copy the structure of the web security tables to use with Code First, there is an article .... which I cannot find at the moment ... give me a few.

好吧,我找不到它 - 但老实说 - 这很痛苦.最简单的方法,因为您已经生成了数据库,可能是使用 Code First 逆向工程工具,例如 实体框架强力工具.这将为您完成大部分工作.然后只需将类添加到您的 DbContext,创建迁移,并更新您的真实数据库.

Well I could not find it - but honestly - it was a pain. The easiest way, since you have the DB generated already, might be to use an Code First reverse engineer tool like Entity Framework Power Tools. This will do most of the work for you. Then just add the classes to your DbContext, create a migration, and update your real database.

此外 - 您可能需要进行更多修改 - 取决于您的上下文名称等.

Also - you may need to make more modifications than this - depending on your Context name and such.

这篇关于有没有办法让 EF 5 代码首先迁移使用 ASP.NET MVC 4 中的 sql server 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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