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.
 
 
 
 
 
 

60 lines
2.4 KiB

  1. using System;
  2. using System.Data.Entity;
  3. using System.Data.Entity.Infrastructure;
  4. using System.Data.Entity.Migrations;
  5. using System.Data.Entity.ModelConfiguration;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Reflection;
  9. namespace Learun.DataBase.SqlServer
  10. {
  11. /// <summary>
  12. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  13. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  14. /// 创建人:陈彬彬
  15. /// 日 期:2017.03.04
  16. /// 描 述:数据访问(SqlServer) 上下文
  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 SqlConnection(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. /// <summary>
  36. /// 模型创建重载
  37. /// </summary>
  38. /// <param name="modelBuilder">模型创建器</param>
  39. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  40. {
  41. System.Data.Entity.Database.SetInitializer<DatabaseContext>(null);
  42. string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Learun.DataBase.SqlServer.DLL", "Learun.Application.Mapping.DLL").Replace("file:///", "");
  43. Assembly asm = Assembly.LoadFile(assembleFileName);
  44. var typesToRegister = asm.GetTypes()
  45. .Where(type => !String.IsNullOrEmpty(type.Namespace))
  46. .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
  47. foreach (var type in typesToRegister)
  48. {
  49. dynamic configurationInstance = Activator.CreateInstance(type);
  50. modelBuilder.Configurations.Add(configurationInstance);
  51. }
  52. base.OnModelCreating(modelBuilder);
  53. }
  54. #endregion
  55. }
  56. }