57 lines
2.2 KiB
C#
Raw Normal View History

2025-08-13 11:14:39 +08:00
using MySql.Data.MySqlClient;
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
namespace Learun.DataBase.MySqlEx
{
/// <summary>
/// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
/// Copyright (c) 2013-2018 Hexagon PPM
/// 创建人:研发部
/// 日 期2017.03.04
/// 描 述:数据访问(MySql) 上下文
/// </summary>
public class DatabaseContext : DbContext, IDisposable, IObjectContextAdapter
{
#region
/// <summary>
/// 初始化一个 使用指定数据连接名称或连接串 的数据访问上下文类 的新实例
/// </summary>
/// <param name="connString"></param>
public DatabaseContext(string connString)
: base(new MySqlConnection(connString), true)
{
this.Configuration.AutoDetectChangesEnabled = false;
this.Configuration.ValidateOnSaveEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
#endregion
#region
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
System.Data.Entity.Database.SetInitializer<DatabaseContext>(null);
string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Learun.DataBase.MySqlEx.DLL", "Learun.Application.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);
}
#endregion
}
}