使用Effort的XML列

时间:2016-08-31 13:49:52

标签: entity-framework effort

我使用Effort框架伪造了我的Entity Framework上下文。我正在使用XML类型的列来存储数据。显然Effort无法处理这个问题。我怎么能绕过这个?欢迎所有建议!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我编写了一些代码,通过实体框架模型并从模型中删除了xml列类型。

以下是如何使用它的示例。只需为主代码创建一个新的包装器DbContext类,然后使用它。

   public class EffortDbContext : OriginalContext
    {
        public EffortDbContext(DbConnection connection) : base(connection, false)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            RemoveXmlColumnTypeFromModelBuilder(modelBuilder);
        }

        /// <summary>
        /// Removes the XML column type from model builder.
        /// </summary>
        /// <remarks>Beware in using this code, here be dragons.  It meddles with the internals of the entity model configuration to strip out the XML column type.</remarks>
        /// <param name="modelBuilder">The model builder.</param>
        private void RemoveXmlColumnTypeFromModelBuilder(DbModelBuilder modelBuilder)
        {
            var _modelConfiguration = modelBuilder.GetType()
                .GetField("_modelConfiguration", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(modelBuilder);

            var _entityConfigurations = (IEnumerable) _modelConfiguration.GetType()
                .GetField("_entityConfigurations", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(_modelConfiguration);

            foreach (object configuration in _entityConfigurations)
            {
                var entityConfigurationDictionaryValue = configuration.GetType().GetProperty("Value").GetValue(configuration);


                var ppc = (IEnumerable) entityConfigurationDictionaryValue.GetType()
                    .GetProperty("PrimitivePropertyConfigurations", BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(entityConfigurationDictionaryValue);
                foreach (var primitivePropertyConfiguration in ppc)
                {
                    var primitivePropertyConfigurationValue = primitivePropertyConfiguration.GetType().GetProperty("Value").GetValue(primitivePropertyConfiguration);
                    var columnTypeProperty = primitivePropertyConfigurationValue.GetType().GetProperty("ColumnType");
                    if (columnTypeProperty.GetValue(primitivePropertyConfigurationValue)?.ToString() == "xml")
                        columnTypeProperty.SetValue(primitivePropertyConfigurationValue, null);
                }
            }
        }
    }

希望这有帮助。