You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using EFCore7Study.DataService.Models;
|
|
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace EFCore7Study.DataService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 多构造函数
|
|
|
|
|
/// 使用工厂对象(AddDbContextFactory)时,因多构建函数,IoC 获取对象时会异常
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AppDbContext3 : DbContext
|
|
|
|
|
{
|
|
|
|
|
private string? _connectString;
|
|
|
|
|
|
|
|
|
|
public AppDbContext3()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AppDbContext3(string? connectstring)
|
|
|
|
|
{
|
|
|
|
|
_connectString = connectstring;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AppDbContext3(DbContextOptions<AppDbContext> options)
|
|
|
|
|
: base(options)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
}
|