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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
using EFCore7Study.DataService.Models ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.Extensions.DependencyInjection ;
using EntityFrameworkCore.UseRowNumberForPaging ;
namespace EFCore7Study.DataService
{
/// <summary>
/// 多构造函数
/// 为使用工厂方式注册,加 ActivatorUtilitiesConstructor 特性
/// </summary>
public class AppDbContext : DbContext
{
/// <summary>
/// 连接字符串, 可实现属性设置, 避免多构造函数在Factory时, IoC异常
/// </summary>
public string? ConnectString = @"Server=127.0.0.1\SQL2019;Database=EFCore7Study;User Id=sa;Password=gly-bicijinlian;Encrypt=True;TrustServerCertificate=True;" ;
public AppDbContext ( )
{
}
public AppDbContext ( 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 , opt = > { opt . UseRowNumberForPaging ( ) ; } )
. EnableSensitiveDataLogging ( ) ;
}
}
protected override void OnModelCreating ( ModelBuilder modelBuilder )
{
modelBuilder . Entity < Account > ( ) . ToTable ( "Account" ) ;
base . OnModelCreating ( modelBuilder ) ;
}
public DbSet < Account > Accounts { get ; set ; }
}
}