EF 迁移控制日志记录 SQL

Posted

技术标签:

【中文标题】EF 迁移控制日志记录 SQL【英文标题】:EF migration control logging SQL 【发布时间】:2018-08-13 15:51:09 【问题描述】:

我有一些用于创建数据库和应用迁移的代码:

public static (Server Server, string ConnectionString) InitializeServerAndDatabase(string databaseName, string defaultConnectionConnectionString, DbMigrationsConfiguration migrationsConfiguration)

    var sqlConnection = new SqlConnection(defaultConnectionConnectionString);
    var serverConnection = new ServerConnection(sqlConnection);
    var server = new Server(serverConnection);
    var database = new Database(server, databaseName);
    database.Create();

    // Build database with migrations and seed data
    var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(defaultConnectionConnectionString);
    sqlConnectionStringBuilder.InitialCatalog = databaseName;
    var connectionString = sqlConnectionStringBuilder.ToString();
    migrationsConfiguration.TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient");
    var migrator = new DbMigrator(migrationsConfiguration);
    var logger = new MigratorLoggingDecorator(migrator, new MinimalMigrationLogger());
    logger.Update();

    // Set environment variable so the DbContext will establish a connection to the right database
    Environment.SetEnvironmentVariable("DefaultConnection", connectionString);

    return (server, connectionString);

由于运行迁移记录的 SQL 比我想要的多得多,我尝试通过编写 MinimalMigrationsLogger 来最小化日志记录,这在上面的方法中使用:

public class MinimalMigrationLogger : MigrationsLogger

    public override void Info(string message)
    
        // Ignore it; there's too much of it clogging up CI
    

    public override void Verbose(string message)
    
        // The SQL text and other info comes here
        // Ignore it; there's too much of it clogging up CI
    

    public override void Warning(string message)
    
        Console.WriteLine(message);
    

但是,我的日志中仍然有用于创建表和种子数据的 SQL。为什么我的设置不能避免这种情况?如何更改它使其不会记录表创建和种子数据 SQL?

【问题讨论】:

我试过你的例子,如果我覆盖 Verbose 我看不到创建脚本... 【参考方案1】:

尝试以下完整示例,看看与您的示例有何不同。 您所要做的就是在同一个项目上创建迁移

using Microsoft.SqlServer.Management.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;

namespace EntityFramework.ConsoleExample

    using Microsoft.SqlServer.Management.Smo;

    class Program
    
        public static string ServerName = "localhost";
        public static string DbName = "EntityFramework.ConsoleExample.MyContenxt";
        public static string ConnectionString = @"Server=" + ServerName + "; Database=" + DbName + @";Integrated Security = True;MultipleActiveResultSets=true";

        static void Main(string[] args)
        
            var conf = new EntityFramework.Console.Migrations.Configuration
            
                MigrationsAssembly = typeof(Customer).Assembly
            ;

            InitializeServerAndDatabase(DbName, ServerName, Program.ConnectionString, conf);
            System.Console.ReadLine();
        

        public static void InitializeServerAndDatabase(string databaseName, string serverName, string defaultConnectionConnectionString, DbMigrationsConfiguration migrationsConfiguration)
        
            ServerConnection sqlConnection = new ServerConnection(serverName);
            Server sqlServer = new Server(sqlConnection);
            Database newDB = new Database(sqlServer, databaseName);
            newDB.Create();

            migrationsConfiguration.TargetDatabase = new DbConnectionInfo(defaultConnectionConnectionString, "System.Data.SqlClient");
            var migrator = new DbMigrator(migrationsConfiguration);
            var logger = new MigratorLoggingDecorator(migrator, new MinimalMigrationLogger());

            logger.Update();
        
    

    public class Customer
    
        public int CustomerId  get; set; 
        public int Age  get; set; 
        public string Name  get; set; 
        public string Name2  get; set; 
        public string Name3  get; set; 
    

    public class MyContenxt : DbContext
    
        public DbSet<Customer> Customers  get; set; 
    

    public class MinimalMigrationLogger : MigrationsLogger
    
        public override void Info(string message)
        
            System.Console.WriteLine("Info::::" + message);
        

        public override void Verbose(string message)
        
            //System.Console.WriteLine("Verbose::::" + message);
        

        public override void Warning(string message)
        
            System.Console.WriteLine("Warning::::" + message);
        
    

【讨论】:

以上是关于EF 迁移控制日志记录 SQL的主要内容,如果未能解决你的问题,请参考以下文章

SVN迁移到GitLab

在控制台中禁用 Rails SQL 日志记录

csharp 实体框架核心 - SQL查询控制台日志记录

如何配置 OpenJPA SQL 日志记录?

使用 Spring Boot 记录 Flyway sql

请问在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句?