SQLite Code配置DbConfiguration

时间:2021-12-22 20:39:55
 [DbConfigurationType(typeof(SQLiteConfiguration))]
public partial class rsapiEntities : DbContext
{
public rsapiEntities()
: base("name=rsapiEntities")
{ Database.SetInitializer<rsapiEntities>(new MigrateDatabaseToLatestVersion<rsapiEntities, APIDbMigrationsConfiguration>());
//this.Configuration.AutoDetectChangesEnabled = false;//关闭自动跟踪对象的属性变化
this.Configuration.LazyLoadingEnabled = false; //关闭延迟加载
//this.Configuration.ProxyCreationEnabled = false; //关闭代理类
//this.Configuration.ValidateOnSaveEnabled = false; //关闭保存时的实体验证
//this.Configuration.UseDatabaseNullSemantics = true; //关闭数据库null比较行为 //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//dynamically load all configuration
modelBuilder.Configurations.AddFromAssembly(typeof(rsapiEntities).Assembly); //...or do it manually below. For example,
//modelBuilder.Configurations.Add(new PartnersMap());
//modelBuilder.Configurations.Add(new EndpointPolicyMap());
//modelBuilder.Configurations.Add(new ThrottlePolicyMap()); //throw new UnintentionalCodeFirstException();
} public virtual DbSet<partners> partners { get; set; }
public virtual DbSet<endpoint_policy> endpoint_policy { get; set; }
public virtual DbSet<throttle_policy> throttle_policy { get; set; }
} internal sealed class APIDbMigrationsConfiguration : DbMigrationsConfiguration<rsapiEntities>
{
public APIDbMigrationsConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
} public class SQLiteConfiguration : DbConfiguration
{
public SQLiteConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
Type t = Type.GetType("System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6");
FieldInfo fi = t.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static);
SetProviderServices("System.Data.SQLite", (System.Data.Entity.Core.Common.DbProviderServices)fi.GetValue(null));
}
}