Entity Framework 4 Code First - 防止数据库删除/创建

发布于 2024-11-09 11:31:01 字数 508 浏览 0 评论 0原文

我使用 Code First 范例编写了一个 ASP.Net MVC 3 应用程序,当我对模型进行更改时,实体框架会自动尝试通过 DROP 和 CREATE 语句重新创建底层 SQL Server 数据库。问题是应用程序托管在第三方远程服务器上,这限制了我可以拥有的数据库数量,并且似乎不允许我以编程方式执行“CREATE DATABASE...”语句,正如我从以下错误消息中收集到的:

数据库“master”中的 CREATE DATABASE 权限被拒绝。

有什么方法可以阻止实体框架删除并尝试重新创建整个数据库,而是简单地删除表并重新创建它们?

手动创建数据库并运行应用程序后当实体框架尝试修改数据库时,我还收到以下错误:

无法检查模型兼容性,因为数据库不包含模型元数据。确保 IncludeMetadataConvention 已添加到 DbModelBuilder 约定中。

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.

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

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

发布评论

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

评论(2

孤千羽 2024-11-16 11:31:01

更新:通过谷歌找到了这个宝石,听起来它正是您所需要的: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/

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/

倾城泪 2024-11-16 11:31:01

将 EF 4.3 与迁移一起使用,您不会出现这种行为 - 至少我没有看到它。但我的代码中也有这样的设置 -

public sealed class DbConfiguration : DbMigrationsConfiguration<DatabaseContext>
{
    public DbConfiguration()
    {
        AutomaticMigrationsEnabled = false;
    }
}

Using EF 4.3 with Migrations you do not get this behavior - at least I have not seen it. But I also have this set in my code -

public sealed class DbConfiguration : DbMigrationsConfiguration<DatabaseContext>
{
    public DbConfiguration()
    {
        AutomaticMigrationsEnabled = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文