EF Core继承DbContext的上下文类参考,包含跟踪sql日志

using Comm;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug;
using Microsoft.Extensions.Logging.TraceSource;
using Model.Entity;
using Model.View;
using System;
using System.Linq;

namespace DAL
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options)
            : base(options)
        {
        }
 

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // optionsBuilder.UseInternalServiceProvider()            
            //string connection = "";
            //optionsBuilder.UseSqlServer(connection);
            //optionsBuilder.UseSqlServer(optionsBuilder.Options.Extensions.)          

            //this.Database.SetCommandTimeout(60);

            //设置不跟踪所有查询  2019-8-4 15:24:33 add
            optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            //参数记录 2019-8-4 15:24:33 add
            optionsBuilder.EnableSensitiveDataLogging();

            var loggerFactory = new LoggerFactory();
            //  loggerFactory.AddProvider(new ConsoleLoggerProvider((category, level)
            //=> category == DbLoggerCategory.Database.Command.Name
            //   && level == LogLevel.Information, true));
            
            //记录sql生成
            loggerFactory.AddDebug(
                (categoryName, logLevel) =>
                //(logLevel == LogLevel.Debug) && (categoryName == DbLoggerCategory.Database.Command.Name)
                logLevel == LogLevel.Information
                && categoryName == DbLoggerCategory.Database.Command.Name
                //|| (categoryName == DbLoggerCategory.Query.Name)
                //|| (categoryName == DbLoggerCategory.Model.Name)
                );

            optionsBuilder.UseLoggerFactory(loggerFactory);
        }

        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    //modelBuilder.Entity<Student>().Property<string>("TenantId").HasField("_tenantId");
        //     Configure entity filters
        //    //modelBuilder.Entity<Student>().HasQueryFilter(b => EF.Property<string>(b, "TenantId") == _tenantId);
        //    //modelBuilder.Entity<Student>().HasQueryFilter(p => p.DataState != (short)EDataState.Delete);
        //    //modelBuilder.Entity<Media>().HasQueryFilter(p => p.DataState != (short)EDataState.Delete);            
        //}

        public virtual DbSet<Setting> Setting { get; set; }
        public virtual DbSet<Media> Media { get; set; }
        public virtual DbSet<Student> Student { get; set; }

    }

    

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值