Beispiel #1
0
        private static void PopulateEFTableAttribute(Type type, EntityMapping mapping, object att)
        {
            if (EFDataAnnotiationAdapter.Instance == null)
            {
                EFDataAnnotiationAdapter.Init(att.GetType().Assembly);
            }
            var tableName  = EFDataAnnotiationAdapter.Instance.Table.Name(att) as string;
            var schemaName = EFDataAnnotiationAdapter.Instance.Table.Schema(att) as string;

            PopulateTableName(mapping, tableName, type.Name);

            if (schemaName.HasValue())
            {
                mapping.schema = schemaName;
            }
        }
        public static void Init(Assembly asm)
        {
            asm = AssemblyLoader.Load("System.ComponentModel.DataAnnotations");
            Guard.NotNull(asm, "asm");

            StrNotMappedAttribute         = "System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute";
            StrTableAttribute             = "System.ComponentModel.DataAnnotations.Schema.TableAttribute";
            StrColumnAttribute            = "System.ComponentModel.DataAnnotations.Schema.ColumnAttribute";
            StrDatabaseGeneratedAttribute = "System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute";
            StrForeignKeyAttribute        = "System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute";


            EFDataAnnotiationAdapter.NotMappedAttributeType         = asm.GetType(StrNotMappedAttribute);
            EFDataAnnotiationAdapter.TableAttributeType             = asm.GetType(StrTableAttribute);
            EFDataAnnotiationAdapter.ColumndAttributeType           = asm.GetType(StrColumnAttribute);
            EFDataAnnotiationAdapter.DatabaseGeneratedAttributeType = asm.GetType(StrDatabaseGeneratedAttribute);
            EFDataAnnotiationAdapter.MaxLengthAttributeType         = asm.GetType(StrMaxLengthAttribute);
            EFDataAnnotiationAdapter.ForeignKeyAttributeType        = asm.GetType(StrForeignKeyAttribute);



            var instance = new EFDataAnnotiationAdapter();

            instance.Table        = new TableAttribute();
            instance.Table.Schema = EFDataAnnotiationAdapter.TableAttributeType.GetProperty("Schema").GetGetter();
            instance.Table.Name   = EFDataAnnotiationAdapter.TableAttributeType.GetProperty("Name").GetGetter();

            instance.Column          = new ColumnAttribute();
            instance.Column.Name     = EFDataAnnotiationAdapter.ColumndAttributeType.GetProperty("Name").GetGetter();
            instance.Column.TypeName = EFDataAnnotiationAdapter.ColumndAttributeType.GetProperty("TypeName").GetGetter();

            instance.DatabaseGenerated = new DatabaseGeneratedAttribute();
            instance.DatabaseGenerated.DatabaseGeneratedOption = EFDataAnnotiationAdapter.DatabaseGeneratedAttributeType.GetProperty("DatabaseGeneratedOption").GetGetter();

            instance.ForeignKey      = new ForeignKeyAttribute();
            instance.ForeignKey.Name = EFDataAnnotiationAdapter.ForeignKeyAttributeType.GetProperty("Name").GetGetter();

            instance.MaxLength        = new MaxLengthAttribute();
            instance.MaxLength.Length = EFDataAnnotiationAdapter.MaxLengthAttributeType.GetProperty("Length").GetGetter();
            Instance = instance;
        }
        static DbConfiguration()
        {
            ManualResetEvent mre = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem(s =>
            {
                var dlinqAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p.GetName().Name == "System.Data.Linq");
                if (dlinqAsm != null)
                {
                    ULinq.Init(dlinqAsm);
                }
                var dataAnnotiationAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p.GetName().Name == DataAnnotationMappingAdapter.StrAssemblyName);
                if (dataAnnotiationAsm != null)
                {
                    DataAnnotationMappingAdapter.Init(dataAnnotiationAsm);
                }
                var efDataAnnotiationAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p.GetName().Name == EFDataAnnotiationAdapter.StrAssemblyName);
                if (efDataAnnotiationAsm != null)
                {
                    EFDataAnnotiationAdapter.Init(efDataAnnotiationAsm);
                }
                //初始化PrimitiveMapper
                Converter.IsPrimitiveType(Types.Boolean);
                //初始化Expressor
                var len      = MethodRepository.Len;
                var mappings = UWay.Skynet.Cloud.Data.Common.MethodMapping.Mappings;

                mre.Set();
            }
                                         );

            Options = new Dictionary <string, DbConfigurationInfo>(StringComparer.Ordinal);
            items   = new Dictionary <string, DbConfiguration>(StringComparer.Ordinal);


            Options["MySql.Data.MySqlClient"] = new DbConfigurationInfo
            {
                Driver              = new MySqlDriver(),
                Dialect             = new MySqlDialect(),
                FuncRegistry        = new UWay.Skynet.Cloud.Data.Dialect.Function.MySQL.MySqlFunctionRegistry(),
                DbExpressionBuilder = new MySqlExpressionBuilder(),
                SqlBuilder          = (dialect, funcRegistry) => new MySqlBuilder {
                    Dialect = dialect, FuncRegistry = funcRegistry
                },

                ScriptGenerator = () => new MySQLScriptGenerator(),
                ScriptExecutor  = () => new MySQLScriptExecutor(),
                SchemaLoader    = () => new MySqlSchemaLoader(),
            };

            Options["Oracle.ManagedDataAccess.Client"] = new DbConfigurationInfo
            {
                Driver              = new OracleODPDriver(),
                Dialect             = new OracleDialect(),
                FuncRegistry        = new UWay.Skynet.Cloud.Data.Dialect.Function.Oracle.OracleFunctionRegistry(),
                DbExpressionBuilder = new OracleExpressionBuilder(),
                SqlBuilder          = (dialect, funcRegistry) => new OracleSqlBuilder {
                    Dialect = dialect, FuncRegistry = funcRegistry
                },

                ScriptGenerator = () => new OracleScriptGenerator(),
                ScriptExecutor  = () => new OracleScriptExecutor(),
                SchemaLoader    = () => new OracleSchemaLoader(),
            };

            Options[DbProviderNames.SqlServer] = new DbConfigurationInfo
            {
                Driver              = new SqlServer2005Driver(),
                Dialect             = new MsSql2005Dialect(),
                FuncRegistry        = new UWay.Skynet.Cloud.Data.Dialect.Function.MsSql.MsSql2005FunctionRegistry(),
                DbExpressionBuilder = new MsSql2005ExpressionBuilder(),
                SqlBuilder          = (dialect, funcRegistry) => new SqlServer2005SqlBuilder {
                    Dialect = dialect, FuncRegistry = funcRegistry
                },

                ScriptGenerator = () => new SqlServerScriptGenerator(),
                ScriptExecutor  = () => new SqlServerScriptExecutor(),
                SchemaLoader    = () => new SqlServerSchemaLoader(),
            };

            //Options["System.Data.SQLite"] = new DbConfigurationInfo
            //{
            //    Driver = new SQLiteDriver(),
            //    Dialect = new SQLiteDialect(),
            //    FuncRegistry = new UWay.Skynet.Cloud.Data.Dialect.Function.SQLite.SQLiteFunctionManager(),
            //    DbExpressionBuilder = new SQLiteExpressionBuilder(),
            //    SqlBuilder = (dialect, funcRegistry) => new SQLiteSqlBuilder { Dialect = dialect, FuncRegistry = funcRegistry },

            //    ScriptGenerator = () => new SQLiteScriptGenerator(),
            //    ScriptExecutor = () => new SQLiteScriptExecutor(),
            //    SchemaLoader = () => new SQLiteSchemaLoader(),

            //};

            //Options["System.Data.SqlServerCe.3.5"] = new DbConfigurationInfo
            //{
            //    Driver = new SqlCeDriver(),
            //    Dialect = new SqlCe35Dialect(),
            //    FuncRegistry = new UWay.Skynet.Cloud.Data.Dialect.Function.SqlCe.SqlCeFunctionRegistry(),
            //    DbExpressionBuilder = new SqlCe35ExpressionBuilder(),
            //    SqlBuilder = (dialect, funcRegistry) => new SqlCeBuilder { Dialect = dialect, FuncRegistry = funcRegistry },

            //    ScriptGenerator = () => new SqlCeScriptGenerator(),
            //    ScriptExecutor = () => new SqlCeScriptExecutor(),
            //    SchemaLoader = () => new SqlCeSchemaLoader(),

            //};

            //Options["System.Data.SqlServerCe.4.0"] = new DbConfigurationInfo
            //{
            //    Driver = new SqlCeDriver(),
            //    Dialect = new SqlCe35Dialect(),
            //    FuncRegistry = new UWay.Skynet.Cloud.Data.Dialect.Function.SqlCe.SqlCeFunctionRegistry(),
            //    DbExpressionBuilder = new SqlCe35ExpressionBuilder(),
            //    SqlBuilder = (dialect, funcRegistry) => new SqlCeBuilder { Dialect = dialect, FuncRegistry = funcRegistry },

            //    ScriptGenerator = () => new SqlCeScriptGenerator(),
            //    ScriptExecutor = () => new SqlCeScriptExecutor(),
            //    SchemaLoader = () => new SqlCeSchemaLoader(),

            //};

            //providerNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            //foreach (var item in DbProviderFactories.GetFactoryClasses().Rows.Cast<DataRow>().Select(p => p["InvariantName"] as string))
            //    providerNames.Add(item);

            mre.WaitOne();
            mre.Close();
        }