Ejemplo n.º 1
0
        private static IList <ORMMetadata> GetFluentAPImetdata(Type type, SampleDBContext g)
        {
            IEnumerable <MethodInfo> mfruntime = type.GetRuntimeMethods();

            DbModelBuilder builder = new DbModelBuilder();

            foreach (var rm in mfruntime)
            {
                if (rm.Name == "OnModelCreating")
                {
                    rm.Invoke(g, new object[] { builder });
                }
            }

            var bProp = builder.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).ToList();

            object _modelConfiguration = bProp[1].GetValue(builder, null);

            var modelConfigProp = _modelConfiguration.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name == "ActiveEntityConfigurations");

            var activeEntityConfigurations = modelConfigProp.GetValue(_modelConfiguration, null);

            //Convert Object to list
            var data = (IList)activeEntityConfigurations;


            //Getting FlunetAPI table names with entity name- First Approach Single Entity for single Table
            foreach (var d in data)
            {
                List <ORMMetadata> ormMeatadataList = null;
                var tbleNameConfigured      = d.GetType().GetProperty("IsTableNameConfigured");
                var tbleNameConfiguredValue = tbleNameConfigured.GetValue(d, null);


                if ((bool)tbleNameConfiguredValue)
                {
                    //If tabel is configured in fluentAPI
                    ormMeatadataList = FluentAPISecondApproach(d, "_entityMappingConfigurations");

                    var clrType = d.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name == "ClrType");
                    var item    = clrType.GetValue(d, null);

                    //AssemblyQualifiedName = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

                    var assembly          = item.GetType().GetProperty("AssemblyQualifiedName");
                    var assemblyNameValue = assembly.GetValue(item, null);

                    var namespaceName      = item.GetType().GetProperty("Namespace");
                    var namespaceNamevalue = namespaceName.GetValue(item, null);

                    var EntityName      = item.GetType().GetProperty("Name");
                    var EntityNameValue = EntityName.GetValue(item, null);

                    var tableName      = d.GetType().GetProperty("TableName");
                    var tableNamevalue = tableName.GetValue(d, null);


                    var schemaName      = d.GetType().GetProperty("SchemaName");
                    var schemaNamevalue = schemaName.GetValue(d, null);


                    foreach (var ormMeatadata in ormMeatadataList)
                    {
                        ormMeatadata.Namespace = namespaceNamevalue?.ToString();

                        ormMeatadata.EntityName = EntityNameValue?.ToString();

                        ormMeatadata.Schema = schemaNamevalue?.ToString();

                        ormMeatadata.AssemblyName = assemblyNameValue?.ToString();

                        ExtractAssemblyname(ormMeatadata);

                        mappingList.Add(ormMeatadata);
                    }
                }
                else
                {
                    ORMMetadata ormMeatadata = new ORMMetadata();
                    //If table is not configured in FluentAPI
                    //To get the mapped column over entity property
                    var primitivePropertyConfigurations      = d.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name == "PrimitivePropertyConfigurations");
                    var PrimitivePropertyConfigurationsValue = (IDictionary)primitivePropertyConfigurations.GetValue(d);

                    foreach (var ppc in PrimitivePropertyConfigurationsValue)
                    {
                        var moc = new MemberOfClass();
                        var key = ppc.GetType().GetProperty("Key").GetValue(ppc);

                        var value = ppc.GetType().GetProperty("Value").GetValue(ppc);

                        var columnName = value.GetType().GetProperty("ColumnName").GetValue(value);
                        moc.MappedColumn = columnName.ToString();
                        var querableKeyData = ((IEnumerable)key).AsQueryable();

                        foreach (var q in querableKeyData)
                        {
                            var entityProp = q.GetType().GetProperty("Name").GetValue(q);
                            moc.PropertyName = entityProp.ToString();
                        }
                        ormMeatadata.MemberOfClass.Add(moc);
                    }
                    var clrType = d.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(m => m.Name == "ClrType");
                    var item    = clrType.GetValue(d, null);

                    //AssemblyQualifiedName = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" = "EF6ClassLibraryFluentAPI.Employee, EF6ClassLibraryFluentAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

                    var assembly          = item.GetType().GetProperty("AssemblyQualifiedName");
                    var assemblyNameValue = assembly.GetValue(item, null);

                    var namespaceName      = item.GetType().GetProperty("Namespace");
                    var namespaceNamevalue = namespaceName.GetValue(item, null);

                    var EntityName      = item.GetType().GetProperty("Name");
                    var EntityNameValue = EntityName.GetValue(item, null);

                    var tableName      = d.GetType().GetProperty("TableName");
                    var tableNamevalue = tableName.GetValue(d, null);

                    var schemaName      = d.GetType().GetProperty("SchemaName");
                    var schemaNamevalue = schemaName.GetValue(d, null);


                    ormMeatadata.Namespace = namespaceNamevalue?.ToString();

                    ormMeatadata.EntityName = EntityNameValue?.ToString();

                    ormMeatadata.Schema = schemaNamevalue?.ToString();

                    ormMeatadata.AssemblyName = assemblyNameValue?.ToString();

                    ExtractAssemblyname(ormMeatadata);

                    mappingList.Add(ormMeatadata);
                }
            }

            //returning fluent API metadata

            return(mappingList);
        }