private static void InitMsSql2000(DbConfiguration cfg)
 {
     cfg.Option.Driver              = new SqlServer2000Driver();
     cfg.Option.Dialect             = new MsSql2000Dialect();
     cfg.Option.ScriptExecutor      = () => new SqlServer2000ScriptExecutor();
     cfg.Option.FuncRegistry        = new UWay.Skynet.Cloud.Data.Dialect.Function.MsSql.MsSqlFunctionRegistry();
     cfg.Option.DbExpressionBuilder = new MsSql2000ExpressionBuilder();
 }
Пример #2
0
 public ODPConnectionWrapper(DbConfiguration dbConfiguraiton, DbConnection conn, string @namespace = "Oracle.DataAccess.Client")
     : base(dbConfiguraiton, conn)
 {
     if (_bindByNameProperty == null)
     {
         _bindByNameProperty = conn.GetType().Module.GetType(@namespace + ".OracleCommand").GetProperty("BindByName").GetSetter();
     }
 }
        ///// <summary>
        ///// 构造配置函数
        ///// </summary>
        ///// <param name="connectionStringName"></param>
        ///// <param name="connectionString"></param>
        ///// <param name="providerName"></param>
        ///// <param name="netType"></param>
        ///// <returns></returns>
        //public static DbConfiguration Configure(string connectionStringName, string connectionString, string providerName, NetType netType = NetType.LTE)
        //{
        //    Guard.NotNullOrEmpty(connectionStringName, "connectionStringName");
        //    DbConfiguration cfg;
        //    if (items.TryGetValue(connectionStringName, out cfg))
        //        return cfg;
        //    if (providerName.IsNullOrEmpty())
        //        throw new ConfigurationErrorsException(connectionStringName + " Provider name not exists or invalid for" + connectionStringName);

        //    if (string.IsNullOrEmpty(connectionString))
        //        throw new ConfigurationErrorsException("ConnectionString");


        //    DbProviderFactory factory = null;
        //    try
        //    {
        //        factory = DbProviderFactoriesEx.GetFactory(providerName);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ConfigurationErrorsException(providerName + " Provider name invalid for" + connectionStringName, ex);
        //    }



        //    cfg = new DbConfiguration(providerName, connectionStringName, connectionString, factory);
        //    cfg.AddMappingFile(netType);
        //    lock (items)
        //        items[cfg.Name] = cfg;

        //    AutoMatchDialect(cfg, connectionString, providerName, factory);

        //    return cfg;
        //}

        ///// <summary>
        ///// 构造配置函数
        ///// </summary>
        ///// <param name="connectionStringName"></param>
        ///// <param name="connectionString"></param>
        ///// <param name="providerName"></param>
        ///// <param name="netType"></param>
        ///// <returns></returns>
        //public static DbConfiguration Configure(string connectionStringName, string connectionString, string providerName, NetType netType, Data.DataBaseType databaseType)
        //{
        //    Guard.NotNullOrEmpty(connectionStringName, "connectionStringName");
        //    DbConfiguration cfg;
        //    if (items.TryGetValue(connectionStringName, out cfg))
        //        return cfg;
        //    if (providerName.IsNullOrEmpty())
        //        throw new ConfigurationErrorsException(connectionStringName + " Provider name not exists or invalid for" + connectionStringName);

        //    if (string.IsNullOrEmpty(connectionString))
        //        throw new ConfigurationErrorsException("ConnectionString");


        //    DbProviderFactory factory = null;
        //    try
        //    {
        //        factory = DbProviderFactoriesEx.GetFactory(providerName);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ConfigurationErrorsException(providerName + " Provider name invalid for" + connectionStringName, ex);
        //    }



        //    cfg = new DbConfiguration(providerName, connectionStringName, connectionString, factory);
        //    cfg.AddMappingFile(netType, databaseType);
        //    lock (items)
        //        items[cfg.Name] = cfg;

        //    AutoMatchDialect(cfg, connectionString, providerName, factory);

        //    return cfg;
        //}

        //public static DbConfiguration Configure(string connectionStringName, string connectionString, string providerName)
        //{
        //    Guard.NotNullOrEmpty(connectionStringName, "connectionStringName");
        //    DbConfiguration cfg;
        //    if (items.TryGetValue(connectionStringName, out cfg))
        //        return cfg;
        //    if (providerName.IsNullOrEmpty())
        //        throw new ConfigurationErrorsException(connectionStringName + " Provider name not exists or invalid for" + connectionStringName);

        //    if (string.IsNullOrEmpty(connectionString))
        //        throw new ConfigurationErrorsException("ConnectionString");


        //    DbProviderFactory factory = null;
        //    try
        //    {
        //        factory = DbProviderFactoriesEx.GetFactory(providerName);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ConfigurationErrorsException(providerName + " Provider name invalid for" + connectionStringName, ex);
        //    }



        //    cfg = new DbConfiguration(providerName, connectionStringName, connectionString, factory);
        //    cfg.AddMappingFile(connectionStringName);
        //    lock (items)
        //        items[cfg.Name] = cfg;

        //    AutoMatchDialect(cfg, connectionString, providerName, factory);

        //    return cfg;
        //}

        private static void AutoMatchDialect(DbConfiguration cfg, string connectionString, string providerName, DbProviderFactory factory)
        {
            if (Options.ContainsKey(providerName))
            {
                cfg.Option = Options[providerName];
            }

            PopulateSqlServer2000(cfg, factory);
        }
Пример #4
0
 /// <summary>
 /// 根据dbConfiguration 创建DbContext对象
 /// </summary>
 /// <param name="dbConfiguration"></param>
 public DbContext(DbConfiguration dbConfiguration)
 {
     Guard.NotNull(dbConfiguration, "dbConfiguration");
     InnerContext    = new InternalDbContext(dbConfiguration);
     DbConfiguration = dbConfiguration;
     Connection      = InnerContext.Connection;
     DbHelper        = InnerContext.DbHelper;
     SpHelper        = InnerContext.SpHelper;
     InitializeDbSets();
 }
Пример #5
0
 private InternalDbContext(DbConfiguration dbConfiguration, DbConnection conn, bool hasSelfCreateConnection)
 {
     Driver  = dbConfiguration.Driver;
     Dialect = dbConfiguration.Dialect;
     this.dbConfiguration   = dbConfiguration;
     connection             = conn;
     Operations             = new Dictionary <MemberInfo, List <LambdaExpression> >();
     this.ExpressionBuilder = dbConfiguration.Option.DbExpressionBuilder;
     Log = dbConfiguration.sqlLogger();
     HasSelfCreateConnection = hasSelfCreateConnection;
 }
        private static void AddMappingClass(Assembly assembly, DbConfiguration cfg)
        {
            var types = assembly?.GetTypes();
            var list  = types?.Where(t =>
                                     t.IsClass && !t.IsGenericType && !t.IsAbstract &&
                                     t.GetAttribute <TableAttribute>() != null).ToList();

            if (list != null && list.Any())
            {
                list.ForEach(t =>
                {
                    cfg.AddClass(t);
                });
            }
        }
        internal static DbConfiguration Get(DbContextOption dbContextOption)
        {
            Guard.NotNull(dbContextOption, "dbContextOption");
            DbConfiguration cfg;

            items.TryGetValue(dbContextOption.Container, out cfg);
            if (cfg == null)
            {
                //自动配置
                cfg = DbConfiguration.Configure(dbContextOption);
            }
            //if(cfg.sqlLogger == null)
            //{
            //    cfg.sqlLogger = dbContextOption.Logger;
            //}
            return(cfg);
        }
 private static void PopulateSqlServer2000(DbConfiguration cfg, DbProviderFactory factory)
 {
     if (factory is System.Data.SqlClient.SqlClientFactory)
     {
         var connectionStringBuilder = factory.CreateConnectionStringBuilder();
         connectionStringBuilder.ConnectionString = cfg.ConnectionString;
         connectionStringBuilder["Database"]      = "master";
         using (var conn = factory.CreateConnection())
         {
             conn.ConnectionString = connectionStringBuilder.ConnectionString;
             conn.Open();
             var serverVersion = conn.ServerVersion;
             var version       = int.Parse(serverVersion.Substring(0, 2));
             if (version < 9)
             {
                 InitMsSql2000(cfg);
             }
         }
     }
 }
Пример #9
0
        private void InitializeDbSets()
        {
            var type = GetType();

            if (type == typeof(InternalDbContext))
            {
                return;
            }

            List <MemberInitializer> items;

            if (!MemberInitializersGroup.TryGetValue(DbConfiguration.Name, out items))
            {
                items = new List <MemberInitializer>();
                foreach (var item in type.GetFields().Where(p => typeof(IDbSet).IsAssignableFrom(p.FieldType)))
                {
                    var initiazer = new MemberInitializer();
                    initiazer.EntityModel     = DbConfiguration.GetMapping(ReflectionHelper.GetElementType(item.FieldType));
                    initiazer.Setter          = item.GetSetter();
                    initiazer.DbSetConstrctor = typeof(DbSet <>).MakeGenericType(initiazer.EntityModel.EntityType).GetConstructors().FirstOrDefault().GetCreator();
                    items.Add(initiazer);
                }
                foreach (var item in type.GetProperties().Where(p => typeof(IDbSet).IsAssignableFrom(p.PropertyType)).Where(p => p.CanWrite))
                {
                    var initiazer = new MemberInitializer();
                    initiazer.EntityModel     = DbConfiguration.GetMapping(ReflectionHelper.GetElementType(item.PropertyType));
                    initiazer.Setter          = item.GetSetter();
                    initiazer.DbSetConstrctor = typeof(DbSet <>).MakeGenericType(initiazer.EntityModel.EntityType).GetConstructors().FirstOrDefault().GetCreator();
                    items.Add(initiazer);
                }
                lock (MemberInitializersGroup)
                    MemberInitializersGroup[DbConfiguration.Name] = items;
            }

            foreach (var item in items)
            {
                item.Init(this);
            }
        }
 /// <summary>
 /// 配置SQLite
 /// </summary>
 /// <param name="databaseFile"></param>
 /// <param name="password"></param>
 /// <param name="failIfMissing"></param>
 /// <param name="isDefault"></param>
 /// <returns></returns>
 public static DbConfiguration ConfigureSQLite(string databaseFile, string password, bool failIfMissing, bool isDefault)
 {
     return(DbConfiguration.Configure(BuildSQLiteConnectionString(databaseFile, password, failIfMissing), DbProviderNames.SQLite, isDefault));
 }
        /// <summary>
        /// 通过DbConnection对象创建DbConfiguration对象
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="entityAssembly"></param>
        /// <param name="containerName"></param>
        /// <param name="mappingFile"></param>
        /// <param name="isDefault"></param>
        /// <returns></returns>
        public static DbConfiguration Configure(string containerName, DbConnection conn, string entityAssembly, string mappingFile, bool isDefault)
        {
            Guard.NotNull(conn, "conn");
            var providerType = conn.GetType()
                               .Assembly
                               .GetTypes()
                               .Where(t => typeof(System.Data.Common.DbProviderFactory).IsAssignableFrom(t) &&
                                      t.Namespace == conn.GetType().Namespace)
                               .FirstOrDefault()
            ;

            if (providerType == null)
            {
                throw new NotSupportedException("not found 'DbProviderFactory'");
            }
            var factory = providerType.GetField("Instance", BindingFlags.Public | BindingFlags.Static).GetValue(null) as DbProviderFactory;

            Guard.NotNull(factory, "factory");

            var providerName = providerType.Namespace;
            var cfg          = new DbConfiguration(providerName, containerName, conn.ConnectionString, isDefault, factory);

            if (!Options.ContainsKey(providerName))
            {
                var dbtype = conn.GetType().Name;
                if (dbtype.StartsWith("MySql"))
                {
                    providerName = DbProviderNames.MySQL;
                }
                else if (dbtype.StartsWith("SqlCe"))
                {
                    providerName = DbProviderNames.SqlCe35;
                }
                else if (dbtype.StartsWith("Oledb"))
                {
                    providerName = DbProviderNames.Oledb;
                }
                else if (dbtype.StartsWith("Oracle"))
                {
                    providerName = DbProviderNames.Oracle;
                }
                else if (dbtype.StartsWith("SQLite"))
                {
                    providerName = DbProviderNames.SQLite;
                }
                else if (dbtype.StartsWith("System.Data.SqlClient."))
                {
                    providerName = DbProviderNames.SqlServer;
                }
            }
            if (!string.IsNullOrEmpty(mappingFile))
            {
                cfg.AddFile(mappingFile);
            }
            else if (!string.IsNullOrEmpty(entityAssembly))
            {
                AddMappingClass(Assembly.Load(entityAssembly), cfg);
            }
            else
            {
                AddMappingClass(Assembly.GetExecutingAssembly(), cfg);
            }

            PopulateSqlServer2000(conn, factory, cfg);
            cfg.connection = conn;
            return(cfg);
        }
 /// <summary>
 ///  配置Oracle ODP
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="isDefault"></param>
 /// <returns></returns>
 public static DbConfiguration ConfigureOracleODP(string connectionString, bool isDefault)
 {
     return(DbConfiguration.Configure(connectionString, DbProviderNames.Oracle_ODP, isDefault));
 }
 /// <summary>
 /// 配置SqlServer
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="isDefault"></param>
 /// <returns></returns>
 public static DbConfiguration ConfigureSqlServer(string connectionString, bool isDefault)
 {
     return(DbConfiguration.Configure(connectionString, DbProviderNames.SqlServer, isDefault));
 }
 /// <summary>
 /// 配置MySQL
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="isDefault"></param>
 /// <returns></returns>
 public static DbConfiguration ConfigureMySQL(string connectionString, bool isDefault)
 {
     return(DbConfiguration.Configure(connectionString, DbProviderNames.MySQL, isDefault));
 }
Пример #15
0
 internal InternalDbContext(DbConfiguration dbConfiguration, DbConnection conn)
     : this(dbConfiguration, conn, false)
 {
 }
Пример #16
0
 /// <summary>
 /// 根据dbConfigurationName 创建DbContext对象
 /// </summary>
 /// <param name="dbConfigurationName"></param>
 public DbContext(DbContextOption dbContextOption) : this(DbConfiguration.Get(dbContextOption))
 {
 }
        /// <summary>
        /// 通过connectionStringName对象创建DbConfiguration对象(可以用于配置文件中有多个数据库连接字符串配置)
        /// </summary>
        /// <param name="dbContextOption"></param>
        /// <returns></returns>
        public static DbConfiguration Configure(DbContextOption dbContextOption)
        {
            Guard.NotNullOrEmpty(dbContextOption.Container, "connectionStringName");
            DbConfiguration cfg;

            if (items.TryGetValue(dbContextOption.Container, out cfg))
            {
                if (cfg.sqlLogger == null)
                {
                    cfg.SetSqlLogger(() => dbContextOption.LogggerFactory.CreateLogger(dbContextOption.Container));
                }
                return(cfg);
            }


            //var item = UCloudConfiguration.Current.GetConnectionString(connectionStringName);

            if (string.IsNullOrEmpty(dbContextOption.Provider))
            {
                throw new ConfigurationErrorsException("connectionString.ProviderName");
            }
            var connectionString = dbContextOption.ConnectionString;

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ConfigurationErrorsException("ConnectionString");
            }
            DbProviderFactory factory = null;

            try
            {
                factory = DbProviderFactoriesEx.GetFactory(dbContextOption.Provider);
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException(dbContextOption.Provider + " Provider name invalid for" + dbContextOption.Container, ex);
            }



            cfg = new DbConfiguration(dbContextOption.Provider, dbContextOption.Container, connectionString, dbContextOption.Default, factory);
            if (!string.IsNullOrEmpty(dbContextOption.MappingFile))
            {
                cfg.AddFile(dbContextOption.MappingFile);
            }
            else if (!string.IsNullOrEmpty(dbContextOption.ModuleAssemblyName))
            {
                var assembly = Assembly.Load(dbContextOption.ModuleAssemblyName);
                var types    = assembly?.GetTypes();
                var list     = types?.Where(t =>
                                            t.IsClass && !t.IsGenericType && !t.IsAbstract &&
                                            t.GetAttribute <TableAttribute>() != null).ToList();
                if (list != null && list.Any())
                {
                    list.ForEach(t =>
                    {
                        cfg.AddClass(t);
                    });
                }
            }

            cfg.SetSqlLogger(() => dbContextOption.LogggerFactory.CreateLogger(dbContextOption.Container));

            lock (items)
                items[cfg.Name] = cfg;

            AutoMatchDialect(cfg, connectionString, dbContextOption.Provider, factory); if (dbContextOption.ConnectionString != null)
            {
                cfg.MakeDefault();
            }

            return(cfg);
        }
Пример #18
0
 public ManagedODPConnectionWrapper(DbConfiguration dbConfiguraiton, DbConnection conn)
     : base(dbConfiguraiton, conn, "Oracle.ManagedDataAccess.Client")
 {
 }
Пример #19
0
 /// <summary>
 /// 把当前DbConfiguration标志为缺省DbConfiguration
 /// </summary>
 /// <returns></returns>
 public DbConfiguration MakeDefault()
 {
     DefaultDbConfiguration = this;
     return(this);
 }
Пример #20
0
        /// <summary>
        /// 得到工作单元对象
        /// </summary>
        /// <param name="dbContextOption">配置信息</param>
        /// <returns></returns>
        public static IDbContext Get(DbContextOption dbContextOption)
        {
            Guard.NotNullOrEmpty(dbContextOption.Container, "dbConfigurationName");

            return(DbConfiguration.Configure(dbContextOption).CreateDbContext());
        }
Пример #21
0
 public InternalDbContext(DbConfiguration dbConfiguration)
     : this(dbConfiguration, dbConfiguration.CreateDbConnection(), true)
 {
 }