实体框架 - 使用自定义默认架构数据库脚本生成的实体框架代码优先 - 引用 dbo



我有一个DbContext实现(数据存储库模式),其中我更改了默认模式,如下所示:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("tm");
        base.OnModelCreating(modelBuilder);
    }

我在包管理器控制台中运行以下命令,以生成用于创建数据库的.sql脚本:

    Enable-Migrations -ConnectionStringName "EfDataRepository"
    Add-Migration Initial -ConnectionStringName "EfDataRepository"
    Update-Database -ConnectionStringName "EfDataRepository" -Script -SourceMigration:0

.. 它给了我以下脚本:

DECLARE @CurrentMigration [nvarchar](max)
IF object_id('[dbo].[__MigrationHistory]') IS NOT NULL
    SELECT @CurrentMigration =
        (SELECT TOP (1) 
        [Project1].[MigrationId] AS [MigrationId]
        FROM ( SELECT 
        [Extent1].[MigrationId] AS [MigrationId]
        FROM [dbo].[__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = N'xxxxxxxx'
        )  AS [Project1]
        ORDER BY [Project1].[MigrationId] DESC)
IF object_id('[tm].[__MigrationHistory]') IS NOT NULL
    SELECT @CurrentMigration =
        (SELECT TOP (1) 
        [Project1].[MigrationId] AS [MigrationId]
        FROM ( SELECT 
        [Extent1].[MigrationId] AS [MigrationId]
        FROM [tm].[__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = N'xxxxxxxx'
        )  AS [Project1]
        ORDER BY [Project1].[MigrationId] DESC)
IF @CurrentMigration IS NULL
    SET @CurrentMigration = '0'
IF @CurrentMigration < '201607151403491_Initial'
BEGIN
    IF schema_id('tm') IS NULL
        EXECUTE('CREATE SCHEMA [tm]')
    CREATE TABLE [tm].[Settings] (
        [Id] [int] NOT NULL IDENTITY,
        [Key] [nvarchar](max) NOT NULL,
        [Value] [nvarchar](max),
        CONSTRAINT [PK_tm.Settings] PRIMARY KEY ([Id])
    )
    ... and so on

一切都很好...除了,为什么它会生成一个块,用于检查 dbo 架构中的__MigrationHistory?!然后它正确地继续检查我的自定义架构中的__MigrationHistory(正确的行为)。

目前,我只是删除与dbo相关的块,因为我真的想将我的模式与默认架构的任何交互隔离开来。当我生成脚本时,我不希望它在"tm"架构之外做任何事情。

为什么 EF 以这种方式生成脚本?当脚本的其余部分从未将任何内容放在那里时,真的有任何理由从 dbo 中查询__MigrationHistory吗?我是不是误会了什么?

有一种移动__MigrationHistory的技术。该表具有自己的上下文(System.Data.Entity.Migrations.History.HistoryContext),您可以覆盖该上下文,然后在 OnModelCreation 中更改架构:

public class MyHistoryContext : HistoryContext 
{ 
    public MyHistoryContext(DbConnection dbConnection, string defaultSchema) 
        : base(dbConnection, defaultSchema) 
    { 
    } 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        base.OnModelCreating(modelBuilder); 
        modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin"); 
        modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID"); 
    } 
} 

然后你需要注册它:

public class ModelConfiguration : DbConfiguration 
{ 
    public ModelConfiguration() 
    { 
        this.SetHistoryContext("System.Data.SqlClient", 
            (connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema)); 
    } 
} 

我已将此作为问题记录在 EF 代码复合项目页面上。他们通过以下解释解决了这个问题:

它正在寻找以前版本的历史记录表,该版本可能 一直在 DBO 架构中,这是设计使然,以帮助支持移动 它。

最新更新