使用Effort的XML列

effort entity-framework

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

热门答案

我遇到了同样的问题,我编写了一些代码来完成实体框架模型并从模型中删除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);
                }
            }
        }
    }

希望这可以帮助。




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因