C# EntityFramework 6.0 CreateDatabaseIfNotExists 代码先创建数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19546241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database
提问by Sanj
What am I doing wrong. I have got a user DbContext setup and working when I originally created the Code-First with powershell it all worked fine.
我究竟做错了什么。当我最初使用 powershell 创建 Code-First 时,我有一个用户 DbContext 设置和工作,它一切正常。
I implemented Database Initializer as expected on application start.
我在应用程序启动时按预期实现了数据库初始化程序。
Database.SetInitializer<UserDbContext>(new CreateDatabaseIfNotExists<UserDbContext>());
Just to test out if it really creates the database I actually dropped the database and now I am stuck the database will not be created. I am using SQL Server 2012, any idea what could be wrong.
只是为了测试它是否真的创建了数据库,我实际上删除了数据库,现在我卡住了,不会创建数据库。我正在使用 SQL Server 2012,不知道有什么问题。
The error message I am getting is
我收到的错误消息是
System.InvalidOperationException: Migrations is enabled for context 'UserDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.
I have tried the same from Package Manager console and it is still give me the same message.
我已经从包管理器控制台尝试了相同的方法,它仍然给我相同的消息。
采纳答案by Sanj
Finally figured the solutions, not sure why or what. Changed my Database initializer to MigrateDatabaseToLatestVersion instead of CreateDatabaseIfNotExists worked.
终于找到了解决方案,不知道为什么或什么。将我的数据库初始值设定项更改为 MigrateDatabaseToLatestVersion 而不是 CreateDatabaseIfNotExists 工作。
Database.SetInitializer<UserDbContext>(new MigrateDatabaseToLatestVersion<UserDbContext, Configuration>());
回答by Matthew Verstraete
Edit: With your new error message the problem comes from you having migrations enabled and already ran a migration (probably the first creation of the database) and since you dropped the DB the migration history has been lost. If your not using Automatic migrations you can not go in and make changes to the database your self and expect code-first to know about it. Try disabling migrations and re-enabling them to see if that clears out the migration history.
编辑:对于您的新错误消息,问题来自您启用了迁移并且已经运行了迁移(可能是第一次创建数据库),并且由于您删除了数据库,迁移历史已经丢失。如果您不使用自动迁移,则无法自行更改数据库并期望代码优先了解它。尝试禁用迁移并重新启用它们以查看是否清除了迁移历史记录。
You will need to make a call into the DB either as a read or insert of data for the DB to initially be created. The code you use only tells EF how to deal with a database if one does not exist when it tries to find it. For me I use the method outlined at http://www.codeproject.com/Tips/411288/Ensure-Your-Code-First-DB-is-Always-Initialized
您将需要调用 DB 作为最初创建的 DB 的读取或插入数据。您使用的代码仅告诉 EF 在尝试查找数据库时如何处理不存在的数据库。对我来说,我使用http://www.codeproject.com/Tips/411288/Ensure-Your-Code-First-DB-is-Always-Initialized 中概述的方法