实体框架代码首先访问CSSpace

时间:2013-11-24 17:49:23

标签: c# entity-framework ef-code-first code-first

有没有办法首先如何在实体框架6代码中实际访问CSSpace?

我尝试将DbContext转换为ObjectContext,如下所示:

 public static ObjectContext ToObjectContext(this DbContext dbContext)
        {
            return ((IObjectContextAdapter)dbContext).ObjectContext;
        }

但是如何通过ObjectContext访问CSSpace?

感谢您的回答,任何帮助将不胜感激,我尝试了很多,但似乎没有任何作用:/

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并且遇到了一些在线链接,我调整并构建了下面的扩展方法。 TableMappingColumnMapping只是我用来存储翻译数据的简单类,所以你可以忽略它们。

我从answer here改编了MånsTånneryd代码的片段 (中途下来)。

CSSpace最大的问题是您需要的大部分信息都不会暴露。因此,最有用的部分是使用反射来获取数组和属性数据。希望,这有帮助。如果您需要任何进一步的解释,请随时给我打电话。

      public static IEnumerable<TableMapping> GetMappings(this DbContext context)
            {


  List<TableMapping> tableMappings = new List<TableMapping>();

            var storageMapping = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItem<GlobalItem>(((IObjectContextAdapter)context).ObjectContext.DefaultContainerName, DataSpace.CSSpace);

            dynamic entitySetMaps = storageMapping.GetType().InvokeMember(
                "EntitySetMaps",
                BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
                null, storageMapping, null);

            foreach (var entitySetMap in entitySetMaps)
            {
                var typeMappings = GetArrayList("EntityTypeMappings", entitySetMap);
                dynamic typeMapping = typeMappings[0];
                dynamic types = GetArrayList("Types", typeMapping);

                var fragments = GetArrayList("MappingFragments", typeMapping);
                var fragment = fragments[0];

                var tableSet = GetProperty("TableSet", fragment);
                var tableName = GetProperty("Name", tableSet);

                TableMapping tm = new TableMapping
                {
                    Entity = context.FindContextTableName((string)types[0].Name),
                    Table = tableName
                };

                var properties = GetArrayList("AllProperties", fragment);

                List<ColumnMapping> columnMappings = new List<ColumnMapping>();
                foreach (var property in properties)
                {
                    var edmProperty = GetProperty("EdmProperty", property);
                    var columnProperty = GetProperty("ColumnProperty", property);

                    ColumnMapping cm = new ColumnMapping
                    {
                        Property = edmProperty.Name,
                        Column = columnProperty.Name
                    };
                    columnMappings.Add(cm);
                }

                tm.Columns = columnMappings.AsEnumerable();
                tableMappings.Add(tm);
            }

            return tableMappings.AsEnumerable();
        }

        private static ArrayList GetArrayList(string property, object instance)
        {
            var type = instance.GetType();
            var objects = (IEnumerable)type.InvokeMember(
                property,
                BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, instance, null);
            var list = new ArrayList();
            foreach (var o in objects)
            {
                list.Add(o);
            }
            return list;
        }

        private static dynamic GetProperty(string property, object instance)
        {
            var type = instance.GetType();
            return type.InvokeMember(property, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, instance, null);
        }
相关问题