纽威
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.

35 lines
1.4 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. public NFineDbContext()
  11. : base("NFineDbContext")
  12. {
  13. this.Configuration.AutoDetectChangesEnabled = false;
  14. this.Configuration.ValidateOnSaveEnabled = false;
  15. this.Configuration.LazyLoadingEnabled = false;
  16. this.Configuration.ProxyCreationEnabled = false;
  17. }
  18. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  19. {
  20. string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("NFine.Data.DLL", "NFine.Mapping.DLL").Replace("file:///", "");
  21. Assembly asm = Assembly.LoadFile(assembleFileName);
  22. var typesToRegister = asm.GetTypes()
  23. .Where(type => !String.IsNullOrEmpty(type.Namespace))
  24. .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
  25. foreach (var type in typesToRegister)
  26. {
  27. dynamic configurationInstance = Activator.CreateInstance(type);
  28. modelBuilder.Configurations.Add(configurationInstance);
  29. }
  30. base.OnModelCreating(modelBuilder);
  31. }
  32. }
  33. }