2017-09-05 80 views
7

我正在使用Entity Framework Core 1.1.0進行遷移。當我運行它們時如何使EF核心遷移不再冗長

dotnet ef database update 

在包管理器控制檯中,控制檯充滿了應用的SQL。與此相反,我想僅打印當前正在應用的遷移名稱。我怎樣才能做到這一點?

回答

0

我假定你的項目的配置。

要禁用SQL打印試試這個

var builder = new DbContextOptionsBuilder<NAMEContext>(); 
builder.UseMySql(connectionString); 
builder.UseLoggerFactory(new MigrationLoggerFactory()); <--- this seems to be what you are looking for 
return new MigrationDataContext(builder.Options); 

MigrationLoggerFactory

public class MigrationLoggerFactory : ILoggerFactory 
    { 
     public void Dispose() { } 

     public ILogger CreateLogger(string categoryName) 
     { 
      if ("Microsoft.EntityFrameworkCore.Migrations".Equals(categoryName)) 
       return new MigrationLogger(); 

      return new NullLogger(); 
     } 

     public void AddProvider(ILoggerProvider provider) 
     { 
     } 
    } 

NullLogger

public class NullLogger : ILogger 
    { 
     public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) 
     { 
      throw new NotImplementedException(); 
     } 

     public bool IsEnabled(LogLevel logLevel) 
     { 
      return false; 
     } 

     public IDisposable BeginScope<TState>(TState state) 
     { 
      return null; 
     } 
    } 

這裏是一個article這也可能有幫助