예제 #1
0
        //private ConcurrentDictionary<string, object> _allSet = new ConcurrentDictionary<string, object>();

        public CoralDbContext(DbInitContext context)
            : base(context.ConnectiongString)
        {
            _context = context;
            Configuration.LazyLoadingEnabled   = true;
            Configuration.ProxyCreationEnabled = true;
            if (context.Config.CommandTimeout != 0)
            {
                Database.CommandTimeout = context.Config.CommandTimeout;
            }


            if (context.Config.AutoMerageDataBase)
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion <CoralDbContext, DbMigrationsConfiguration <CoralDbContext> >(true, new DbMigrationsConfiguration <CoralDbContext>
                {
                    AutomaticMigrationsEnabled        = true,
                    AutomaticMigrationDataLossAllowed = false
                }));
            }
            else
            {
                Database.SetInitializer <CoralDbContext>(null);
            }
        }
예제 #2
0
        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual IDbConnection GetConnection(DbInitContext context)
        {
            var connection = Database.DefaultConnectionFactory.CreateConnection(context.ConnectiongString);

            connnections.Add(connection);
            return(connection);
        }
예제 #3
0
        /// <summary>
        /// 获取类型和已配置类型的映射
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Dictionary <Type, Type> GetTypeAndConfigedTypesMapping(DbInitContext context)
        {
            var map = new Dictionary <Type, Type>();

            LinqExtensions.ForEach(MetaDataManager.Type.GetAll(), item =>
            {
                if (item.BaseType == null || !item.BaseType.IsGenericType || item.BaseType.IsAbstract)
                {
                    return;
                }
                if (!IsDefined(item))
                {
                    return;
                }
                var genericType =
                    Enumerable.FirstOrDefault(item.BaseType.GetGenericArguments(), data => data.IsSubclassOf(typeof(Entity)));
                if (genericType == null)
                {
                    return;
                }
                //如果是抽象类或者标记成忽略则无需加载
                if (IgnoreAttribute.IsDefined(genericType) || genericType.IsAbstract)
                {
                    return;
                }
                if (!context.Types.Contains(genericType))
                {
                    return;
                }
                map.Add(genericType, item);
            });
            return(map);
        }
예제 #4
0
 /// <summary>
 /// 创建数据库上下文
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual CoralDbContext Create(DbInitContext context)
 {
     if (context == null)
     {
         throw CoralException.ThrowException <DbErrorCode>(item => item.InvalideDbCoden, "上下文为空");
     }
     if (context.Config == null)
     {
         throw CoralException.ThrowException <DbErrorCode>(item => item.InvalideDbCoden, "上下文配置为空");
     }
     if (string.IsNullOrEmpty(context.Config.NameOrConnectionString))
     {
         throw CoralException.ThrowException <DbErrorCode>(item => item.InvalideDbCoden, "连接字符串为空");
     }
     return(_contexts.GetOrAdd(context.GetIdentity(), item => _creator(item, context)));
 }
예제 #5
0
        /// <summary>
        /// 配置数据库的表和实体之间的映射
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <param name="context"></param>
        public static void ConfigDbTypes(DbModelBuilder modelBuilder, DbInitContext context)
        {
            var configs = GetTypeAndConfigedTypesMapping(context);

            context.Types.ForEach(item =>
            {
                Type type;
                dynamic config;
                if (!configs.TryGetValue(item, out type))
                {
                    config = GeneralDefautConfig(item);
                }
                else
                {
                    config = Activator.CreateInstance(type);
                }
                modelBuilder.Configurations.Add(config);
            });
            ConfigConvertions(modelBuilder.Conventions);
        }