using System; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Reflection; namespace NFine.Data { public class NFineDbContext : DbContext { //使用自定义连接串 private static string GetEFConnctionString() { //string enString = System.Configuration.ConfigurationManager.AppSettings["connstr"]; string connString = Extensions.SqlHelper.DataCenterConnString;// SecurityHelper.DESDecrypt(enString); return connString; } public NFineDbContext() : base(GetEFConnctionString()) { this.Configuration.AutoDetectChangesEnabled = false; this.Configuration.ValidateOnSaveEnabled = false; this.Configuration.LazyLoadingEnabled = false; this.Configuration.ProxyCreationEnabled = false; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("NFine.Data.DLL", "NFine.Mapping.DLL").Replace("file:///", ""); Assembly asm = Assembly.LoadFile(assembleFileName); var typesToRegister = asm.GetTypes() .Where(type => !String.IsNullOrEmpty(type.Namespace)) .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); foreach (var type in typesToRegister) { dynamic configurationInstance = Activator.CreateInstance(type); modelBuilder.Configurations.Add(configurationInstance); } base.OnModelCreating(modelBuilder); } } }