HJ212 and Documents
This commit is contained in:
@@ -1,34 +1,68 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace MachineJob
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库写入上下文
|
||||
/// 用于 Entity Framework Core 访问 MySQL 数据库
|
||||
/// </summary>
|
||||
public class DatabaseWriteContext : DbContext
|
||||
{
|
||||
// 配置根对象
|
||||
private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
// 设置配置文件的基础路径
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
// 添加 appsettings.default.json 配置文件(必需)
|
||||
.AddJsonFile("appsettings.default.json", optional: false, reloadOnChange: true)
|
||||
// 添加 appsettings.json 配置文件(必需)
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
// 根据环境变量添加对应的配置文件(可选)
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
// 从配置读取数据库连接字符串
|
||||
private static readonly string connectionString = configuration.GetConnectionString("DatabaseWriteConnectionString")!;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库写入实体集合
|
||||
/// </summary>
|
||||
public DbSet<DatabaseWriteEntity>? DatabaseWrites { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 配置数据库上下文
|
||||
/// 使用 MySQL 数据库
|
||||
/// </summary>
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
// 使用 MySQL,自动检测服务器版本
|
||||
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据库写入实体类
|
||||
/// 映射到 databasewrites 表
|
||||
/// </summary>
|
||||
[Table(name: "databasewrites")]
|
||||
public partial class DatabaseWriteEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键 ID
|
||||
/// </summary>
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
|
||||
// 注意:Value1 到 Value10 属性由 DatabaseWriteEntityCodeGenerator 代码生成器自动生成
|
||||
// public double? Value1 { get; set; }
|
||||
// public double? Value2 { get; set; }
|
||||
// ...
|
||||
// public double? Value10 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user