gpt4 book ai didi

entity-framework - EF 迁移控制日志记录 SQL

转载 作者:行者123 更新时间:2023-12-04 08:31:20 24 4
gpt4 key购买 nike

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

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?

最佳答案

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

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

关于entity-framework - EF 迁移控制日志记录 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49119816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com