实体框架4代码第一 - 防止DB丢弃/创建 [英] Entity Framework 4 Code First - Prevent DB Drop/Create
问题描述
数据库'master'中拒绝CREATE DATABASE权限。
有没有办法阻止实体框架丢弃并尝试重新创建整个数据库,而是简单地删除表并重新创建它们?
在手动创建数据库并运行应用程序后,我也会看到以下错误,因为Entity Framework尝试修改数据库:
由于数据库不包含模型元数据,因此无法检查模型兼容性。确保将IncludeMetadataConvention添加到DbModelBuilder约定中。
更新: / strong>通过google找到这个宝石,听起来就像你所需要的那样: http://nuget.org/Tags / IDatabaseInitializer
您可以使用其他数据库初始化程序。假设你的上下文被称为 SampleContext
,那么你的构造函数将如下所示:
public SampleContext()
{
System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists< SampleContext>());
}
请注意,以上是默认的初始化程序。您可能需要通过实现 IDatabaseInitializer
来创建自己的自定义初始化程序。这里有一些很好的信息: http: //sankarsan.wordpress.com/2010/10/14/entity-framework-ctp-4-0-database-initialization/
I've written an ASP.Net MVC 3 application using the Code First paradigm whereby when I make a change to the model the Entity Framework automatically attempts to re-create the underlying SQL Server Database via DROP and CREATE statements. The problem is the application is hosted on a 3rd party remote server which limits the number of databases I can have and does not seem to allow me to programmatically execute "CREATE DATABASE..." statements as I gather from this error message:
CREATE DATABASE permission denied in database 'master'.
Is there any way to stop the Entity Framework from dropping and attempting to re-create the whole database and instead make it simply drop the tables and re-create them?
After creating the database manually and running the application I also get the following error I guess as the Entity Framework tries to modify the database:
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
UPDATE: Found this gem through google, it sounds like its exactly what you need: http://nuget.org/Tags/IDatabaseInitializer
You can use a different database initializer. Lets say your context is called SampleContext
then your constructor would look like this:
public SampleContext()
{
System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<SampleContext>());
}
Note that the above is the default initializer. You will probably need to create your own custom initializer by implementing IDatabaseInitializer
. Theres some good info here: http://sankarsan.wordpress.com/2010/10/14/entity-framework-ctp-4-0-database-initialization/
这篇关于实体框架4代码第一 - 防止DB丢弃/创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!