|
|
|
|
using EFCore7Study.DataService.Models;
|
|
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace EFCore7Study.DataService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 多构造函数
|
|
|
|
|
/// 为使用工厂方式注册,合并为一个
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AppDbContext2 : DbContext
|
|
|
|
|
{
|
|
|
|
|
private string? _connectString;
|
|
|
|
|
|
|
|
|
|
//因为先执行的 base(options),到此处时AppDbContext2已经注册完成,连接字符串实际上是用不了的。当然换成其它参数或用在其它地方是可以的。
|
|
|
|
|
public AppDbContext2(DbContextOptions<AppDbContext2> options, string? connectString = null)
|
|
|
|
|
: base(options)
|
|
|
|
|
{
|
|
|
|
|
_connectString = connectString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
if (!optionsBuilder.IsConfigured)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_connectString))
|
|
|
|
|
{
|
|
|
|
|
_connectString = @"Server=127.0.0.1\SQL2019;Database=EFCore7Study;User Id=sa;Password=gly-bicijinlian;Encrypt=True;TrustServerCertificate=True;";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optionsBuilder
|
|
|
|
|
.UseSqlServer(_connectString)
|
|
|
|
|
.EnableSensitiveDataLogging();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
modelBuilder.Entity<Account>().ToTable("Account");
|
|
|
|
|
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbSet<Account> Accounts { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|