123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using Microsoft.EntityFrameworkCore;
- using AmrControl.DB.Models;
- using System.Linq.Expressions;
- using AmrControl.DB.Model;
- namespace AmrControl.DB
- {
- /// <summary>
- /// 数据库上下文配置
- /// </summary>
- public class MyDbContext: DbContext
- {
- /// <summary>
- /// 连接字符串
- /// </summary>
- 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";
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="options"></param>
- #pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
- public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
- #pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
- {
- }
- /// <summary>
- ///
- /// 构造实体
- /// </summary>
- /// <param name="modelBuilder"></param>
- 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);
- }
- }
- /// <summary>
- /// 用户模型
- /// </summary>
- #pragma warning disable IDE1006 // 命名样式
- public DbSet<UserModel> userDb { get; set; }
- public DbSet<Log> logs { get; set; }
- public DbSet<AppConfig> appConfigs { get; set; }
- public DbSet<Models.JGR> jgrs { get; set; }
- public DbSet<Models.Charger> chargers { get; set; }
- public DbSet<Models.Station> stations { get; set; }
- public DbSet<Models.Material> materials { get; set; }
- public DbSet<Models.Storage> storages { get; set; }
- public DbSet<Models.IssueNote> issuenotes { get; set; }
- #pragma warning restore IDE1006 // 命名样式
- }
- }
|