实体框架代码优先迁移中在何处生成CREATE DATABASE语句? [英] Where the CREATE DATABASE statement is generated in Entity Framework Code First with Migrations?

查看:82
本文介绍了实体框架代码优先迁移中在何处生成CREATE DATABASE语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要配置由Entity Framework Code First MigrateDatabaseToLatestVersion类创建的数据库。

I need to configure the database created by Entity Framework Code First MigrateDatabaseToLatestVersion class.

是否有可能影响数据库文件的参数,例如size或maxsize?我特别感兴趣的是,有什么方法可以添加数据库文件吗?

Is it possible to influence the database files parameters like size or maxsize? What interests me in particular, is there a way to add database files?

我假设我需要查找实体框架生成CREATE DATABASE语句并对其产生影响的时刻。一点点。据我了解,SqlServerMigrationSqlGenerator类为时已晚,因为此时数据库已经存在。

I assume I need to find the moment where the Entity Framework generates the CREATE DATABASE statement and influence it a bit. As far as I understand, the SqlServerMigrationSqlGenerator class is too late because the database already exists at this point.

推荐答案

编辑:根据评论,这是行不通的:
您可以使用 Add-Migration 命令添加基于代码的迁移,并修改其 Up 方法执行 Sql( ALTER DATABASE ...)并执行数据库所需的任何操作。 / strike>

According to comment this doesn't work: You can just add code based migration using Add-Migration command and modify its Up method to execute Sql("ALTER DATABASE ...") and do what ever you want with your database.

数据库是通过 DbProviderServices 创建的-这是EF与特定数据库服务器之间的桥接组件。 ObjectContext 公开了用于创建数据库和生成数据库创建脚本的操作。 DbMigrator 在执行 Update 时使用数据库创建操作。它检查数据库是否存在,如果不存在,则使用 ObjectContext.CreateDatabase 。因此,如果要更改数据库创建过程,则需要实现自己的迁移器实现,以更改 Update 方法生成数据库的方式(也许您只需要装饰器,该装饰器将在执行 DbMigrator.Update 操作之前创建一个数据库。

Database is created through DbProviderServices - that is a bridging component between EF and specific database server. ObjectContext exposes operations for creating database and generating database creation script. DbMigrator use database creation operation when you execute Update. It checks that if Database exists and if not it uses ObjectContext.CreateDatabase. So if you want to change the process of creating the database you need to implement your own implementation of the migrator to change the way how Update method generates the database (maybe you just need a decorator which will create a database prior to executing DbMigrator.Update operation).

这篇关于实体框架代码优先迁移中在何处生成CREATE DATABASE语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆