Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

57 строки
2.3 KiB

  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Data.Entity;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Data.Entity.Migrations;
  6. using System.Data.Entity.ModelConfiguration;
  7. using System.Linq;
  8. using System.Reflection;
  9. namespace Learun.DataBase.MySqlEx
  10. {
  11. /// <summary>
  12. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  13. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  14. /// 创建人:陈彬彬
  15. /// 日 期:2017.03.04
  16. /// 描 述:数据访问(MySql) 上下文
  17. /// </summary>
  18. public class DatabaseContext : DbContext, IDisposable, IObjectContextAdapter
  19. {
  20. #region 构造函数
  21. /// <summary>
  22. /// 初始化一个 使用指定数据连接名称或连接串 的数据访问上下文类 的新实例
  23. /// </summary>
  24. /// <param name="connString"></param>
  25. public DatabaseContext(string connString)
  26. : base(new MySqlConnection(connString), true)
  27. {
  28. this.Configuration.AutoDetectChangesEnabled = false;
  29. this.Configuration.ValidateOnSaveEnabled = false;
  30. this.Configuration.LazyLoadingEnabled = false;
  31. this.Configuration.ProxyCreationEnabled = false;
  32. }
  33. #endregion
  34. #region 重载
  35. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  36. {
  37. System.Data.Entity.Database.SetInitializer<DatabaseContext>(null);
  38. string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Learun.DataBase.MySqlEx.DLL", "Learun.Application.Mapping.DLL").Replace("file:///", "");
  39. Assembly asm = Assembly.LoadFile(assembleFileName);
  40. var typesToRegister = asm.GetTypes()
  41. .Where(type => !String.IsNullOrEmpty(type.Namespace))
  42. .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
  43. foreach (var type in typesToRegister)
  44. {
  45. dynamic configurationInstance = Activator.CreateInstance(type);
  46. modelBuilder.Configurations.Add(configurationInstance);
  47. }
  48. base.OnModelCreating(modelBuilder);
  49. }
  50. #endregion
  51. }
  52. }