首先创建一个Context
public partial class OperationDataContext : DbContext
{
public OperationDataContext(DbContextOptions<OperationDataContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#if DEBUG
var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new EFLoggerProvider());
optionsBuilder.UseLoggerFactory(loggerFactory);
#endif
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2");
OnModelCreatingExt(modelBuilder);
}
partial void OnModelCreatingExt(ModelBuilder modelBuilder);
}
然后在配置文件里面加上相关配置
public class DbConfiguration
{
public string OperationDataConfiguration { get; set; } //这里是注入的配置 自己的话配置信息手写
}
然后在项目的Startup文件里 的 public IServiceProvider ConfigureServices(IServiceCollection services)方法中进行配置
.AddDbContext<OperationDataContext>(
options =>
{
options.UseSqlServer(dbconfig.OperationDataConfiguration);
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
})