EF Code First DbConText配置

 public EFDbContext(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            optionsBuilder
                //在控制台中打印sql语句
                .UseLoggerFactory(ConsoleLoggerFactor)

                //配置连接字符串
                .UseSqlServer(configuration.GetConnectionString("DefaultConnectionStrings"));
        }

        //引入实体对象
        public DbSet<ExceptionLog> ExceptionLog { get; set; }
        public DbSet<JoinOrgLog> JoinOrgLog { get; set; }
        public DbSet<LoginLog> LoginLog { get; set; }
        public DbSet<OrderLog> OrderLog { get; set; }
        public DbSet<UpdateInfoLog> UpdateInfoLog { get; set; }


        //配置将Sql语句打印在控制台
        public static readonly ILoggerFactory ConsoleLoggerFactor = LoggerFactory.Create(builder =>
       {
           builder.AddFilter((catergory, level) =>
       catergory == DbLoggerCategory.Database.Command.Name
       && level == LogLevel.Information)
           .AddConsole();
       });
        private readonly IConfiguration configuration;

这样仅需要新建一个EFDbContext  不需要在Startup中再配置相关信息

 

EF Code First DbConText配置

 

EF Code First DbConText配置

上一篇:Mybatis的SQL注入问题学习


下一篇:Centos7.6安装Oracle11g详细教程