在不更改数据库名称的情况下,无法使用 Entity Framework Code First 和 MVC 3 重新创建 SQL Server Express 数据库

发布于 2024-10-12 17:22:54 字数 723 浏览 4 评论 0原文

当我使用以下连接字符串运行应用程序时,数据库文件已成功创建。

<add name="ConnString1"
     connectionString="Data Source=.\SQLEXPRESS;
                       Database=Database1;
                       Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.mdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

如果我删除数据库文件并尝试再次运行应用程序,则无法创建数据库文件,并且出现以下内部异常:

底层提供程序在打开时失败。
{“无法打开登录请求的数据库 \”Database1\”。登录失败。\Database1\n用户“computer\\someuser”登录失败。”}

如果我将 Database=Database1 更改为 < em>Database=Database2 在连接字符串中,则数据库文件创建成功。问题总是重演。

如何在不更改数据库名称的情况下重新创建数据库文件?

When I run the application with the following connection string the database file is created successfully.

<add name="ConnString1"
     connectionString="Data Source=.\SQLEXPRESS;
                       Database=Database1;
                       Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.mdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

If I delete the database file and try to run the application again the database file fails to be created and I get the following inner exceptions:

The underlying provider failed on Open.

{"Cannot open database \"Database1\" requested by the login. The login failed.\Database1\nLogin failed for user 'computer\\someuser'."}

If I change Database=Database1 to Database=Database2 in the connection string then the database file is created successfully. The problem repeats itself always.

How can I recreate the database file without having to change the database name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-10-19 17:22:54

检查以确保目录权限允许您删除 mdf 文件并且登录已删除架构/表权限目录权限是 mdf 文件的常见问题,因为这些文件由于其潜在的敏感性质而具有很高的安全性

Check to make sure the directory rights allow you to delete the mdf file and the the login has drop schema/table privileges directory rights are a common issue with mdf files due the high security placed on these files due to their potentially sensitive nature

ゃ人海孤独症 2024-10-19 17:22:54

我可以看到这是一篇较旧的帖子 - 希望这可以帮助处于同样困境的人。

首先使用代码,应用程序第一次运行时,它构建数据库没有问题 - 它知道它不存在,因为它之前没有构建过它。 Code First 还获取所使用模型的哈希值并将其存储在新数据库中 - 检查名为 EdmMetadata 的表 - 这就是存储哈希值的位置。它使用哈希值随后检查模型在构建之间是否发生了更改,因此它知道是否删除数据库并重建。

删除数据库后第二次,它会查找丢失的数据库来比较模型哈希值,但找不到它,因为数据库现在丢失了。

我的解决方法是向其中一个模型添加一个无意义的字段(记住在开发后删除它)以强制重建,而不删除数据库。或者,您可以只修改数据库哈希值来强制重建。

这适用于代码优先的应用程序数据库 - 对于会员数据库则不太确定。

I can see this is an older post - hopefully this can help someone in the same predicament.

Using code first, the first time the application runs it builds the db no problems - it knows it doesn't exist because it hasn't previously built it. Code first also takes a hash value of the models used and stores that in the new database - check for a table called EdmMetadata - thats where the hash value is stored. It uses the hash value to subsequently check if the model has changed from build to build, so it knows whether to drop the database and rebuild.

The second time through after you've deleted the database, it looks for the missing database to compare the model hash value, and can't find it because the database is now missing.

My workaround is to add a meaningless field (remembering to delete it after development) to one of the models to force the rebuild, without deleting the database. Alternatively, you could just modify the db hash value to force the rebuild.

This works with the code first application databases - not so sure with the membership database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文