MVC 4 - 如何更改默认的SQL Server Express数据库位置

时间:2021-10-01 22:40:49

In VS 2012 I created a new MVC 4 project starting from the internet application template, then changed its properties to use SQL Server Express instead of LocalDB, then tried successfully to register a new user.

在VS 2012中,我从Internet应用程序模板开始创建了一个新的MVC 4项目,然后将其属性更改为使用SQL Server Express而不是LocalDB,然后尝试成功注册新用户。

First, the project created its own membership database in the App_data folder, but for deployment reasons I wanted to change the database location, so I moved the database physical files from C:\MyProject\App_Data\ to C:\MyData and modified the related path in the web.config file. To ensure that the database is reachable by the SQL Server Express instance, I added my own user account with full access in the properties security tab, then successfully shown its data in SQL Server Management Studio.

首先,项目在App_data文件夹中创建了自己的成员资格数据库,但由于部署原因我想更改数据库位置,所以我将数据库物理文件从C:\ MyProject \ App_Data \移动到C:\ MyData并修改了相关内容web.config文件中的路径。为了确保SQL Server Express实例可以访问数据库,我在属性安全选项卡中添加了自己的具有完全访问权限的用户帐户,然后在SQL Server Management Studio中成功显示了其数据。

Now, when I run the project, and click on the "log in" link, the debugger stops at LazyInitializer.EnsureInitialized and fails with an error; the inner exception of the error message says:

现在,当我运行项目并单击“登录”链接时,调试器将停止在LazyInitializer.EnsureInitialized并因错误而失败;错误消息的内部异常说:

Database 'C:\MyProject\App_Data\aspnet-MyProject-20121206123456.mdf' already exists. Choose a different database name.
Cannot attach the file 'C:\MyData\aspnet-MyProject-20121206123456.mdf' as database 'aspnet-MyProject-20121206123456.mdf'

数据库'C:\ MyProject \ App_Data \ aspnet-MyProject-20121206123456.mdf'已经存在。选择其他数据库名称。无法将文件'C:\ MyData \ aspnet-MyProject-20121206123456.mdf'作为数据库'aspnet-MyProject-20121206123456.mdf'附加

I checked all the project to find any different database connection settings but found nothing.

我检查了所有项目以找到任何不同的数据库连接设置,但什么也没找到。

Within VS, when I use the server explorer to view the database tables data, I cannot find "show data" option when I right click on one of the displayed tables, when I go to C:\MyData\ folder to see the physical database files properties, in the security tab, I find that each time I run the project my account using full access was removed.

在VS中,当我使用服务器资源管理器查看数据库表数据时,当我右键单击其中一个显示的表时,我找不到“显示数据”选项,当我转到C:\ MyData \文件夹查看物理数据库时文件属性,在安全选项卡中,我发现每次运行项目时,我的帐户都使用完全访问权限被删除。

Also the browser is displaying the next Exception text:

浏览器也显示下一个异常文本:

The data provider. Net Framework requested was not found. It may not be installed.

C:\MyProject\Filters\InitializeSimpleMembershipAttribute.cs Ligne : 42

C:\ MyProject \ Filters \ InitializeSimpleMembershipAttribute.cs Ligne:42

Ligne 40 :                 }
Ligne 41 : 
Ligne 42 :                     WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
Ligne 43 :                 }
Ligne 44 :                 catch (Exception ex)

1 个解决方案

#1


0  

It looks like a Visual Studio 2012 bug which doesn't recognize SQL Server 2012 Express instance. I have downloaded Microsoft WebMatrix2 and used it to load the website and could successfully log in using the moved membership database.

它看起来像Visual Studio 2012错误,无法识别SQL Server 2012 Express实例。我已经下载了Microsoft WebMatrix2并使用它来加载网站,并且可以使用移动的成员资格数据库成功登录。

With this bug I've lost 2 days trying to find a solution.

有了这个错误,我已经失去了2天试图找到解决方案。

#1


0  

It looks like a Visual Studio 2012 bug which doesn't recognize SQL Server 2012 Express instance. I have downloaded Microsoft WebMatrix2 and used it to load the website and could successfully log in using the moved membership database.

它看起来像Visual Studio 2012错误,无法识别SQL Server 2012 Express实例。我已经下载了Microsoft WebMatrix2并使用它来加载网站,并且可以使用移动的成员资格数据库成功登录。

With this bug I've lost 2 days trying to find a solution.

有了这个错误,我已经失去了2天试图找到解决方案。