using Microsoft.EntityFrameworkCore; using AmrControl.DB.Models; using System.Linq.Expressions; using AmrControl.DB.Model; namespace AmrControl.DB { /// /// 数据库上下文配置 /// public class MyDbContext: DbContext { /// /// 连接字符串 /// private readonly string connStr; public MyDbContext(string connStr) :base() //public MyDbContext() :base() { this.connStr = connStr; //this.connStr = "server=localhost;user=mysqltest;database=ms_jgr;port=3306;password=test1234;Charset=utf8;Pooling=true"; } /// /// 构造函数 /// /// #pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。 public MyDbContext(DbContextOptions options) : base(options) #pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。 { } /// /// /// 构造实体 /// /// protected override void OnModelCreating(ModelBuilder modelBuilder) { //这条语句表示默认从本DLL中检索定义好的模型并创建对应的表,如果不这样做,那就需要在DbContext中定义所有模型并在这个函数中声明模型结构 modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly); base.OnModelCreating(modelBuilder); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); //如果用mysql,则开启下面的语句 //if (this.connStr != null) //{ // optionsBuilder.UseMySql(connStr, ServerVersion.AutoDetect(connStr)); //} //如果用sqlite,则开启下面的语句 if (this.connStr != null) { optionsBuilder.UseSqlite(connStr); } } /// /// 用户模型 /// #pragma warning disable IDE1006 // 命名样式 public DbSet userDb { get; set; } public DbSet logs { get; set; } public DbSet appConfigs { get; set; } public DbSet jgrs { get; set; } public DbSet chargers { get; set; } public DbSet stations { get; set; } public DbSet materials { get; set; } public DbSet storages { get; set; } public DbSet issuenotes { get; set; } #pragma warning restore IDE1006 // 命名样式 } }