EF SQL Migration Management Log Management

I have code for creating a database and applying migration:

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);
}

Since much more SQL was logged to start the migration than I wanted, I tried to minimize logging by writing MinimalMigrationsLoggerwhich is used in the method above:

public class MinimalMigrationLogger : MigrationsLogger
{
    public override void Info(string message)
    {
        // Ignore it; there too much of it clogging up CI
    }

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

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

However, I still get SQL in my logs to create the table and seed data. Why does my installation prevent this? How can I change it so that it does not record table creation and SQL seed data?

+4
source share
1 answer

, .
, ,

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);
        }
    }
}
0

Source: https://habr.com/ru/post/1694495/


All Articles