纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.7 KiB

3 years ago
  1. using System;
  2. using System.Data.Entity;
  3. using System.Data.Entity.ModelConfiguration;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace NFine.Data
  7. {
  8. public class NFineDbContext : DbContext
  9. {
  10. //使用自定义连接串
  11. private static string GetEFConnctionString()
  12. {
  13. //string enString = System.Configuration.ConfigurationManager.AppSettings["connstr"];
  14. string connString = Extensions.SqlHelper.DataCenterConnString;// SecurityHelper.DESDecrypt(enString);
  15. return connString;
  16. }
  17. public NFineDbContext()
  18. : base(GetEFConnctionString())
  19. {
  20. this.Configuration.AutoDetectChangesEnabled = false;
  21. this.Configuration.ValidateOnSaveEnabled = false;
  22. this.Configuration.LazyLoadingEnabled = false;
  23. this.Configuration.ProxyCreationEnabled = false;
  24. }
  25. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  26. {
  27. string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("NFine.Data.DLL", "NFine.Mapping.DLL").Replace("file:///", "");
  28. Assembly asm = Assembly.LoadFile(assembleFileName);
  29. var typesToRegister = asm.GetTypes()
  30. .Where(type => !String.IsNullOrEmpty(type.Namespace))
  31. .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
  32. foreach (var type in typesToRegister)
  33. {
  34. dynamic configurationInstance = Activator.CreateInstance(type);
  35. modelBuilder.Configurations.Add(configurationInstance);
  36. }
  37. base.OnModelCreating(modelBuilder);
  38. }
  39. }
  40. }