XML column using Effort

effort entity-framework

Question

I have my Entity Framework context faked using the Effort framework. I'm using a XML-typed column to store data. Apparently Effort can't handle this. How can I get around this? All suggestions are welcome!

Popular Answer

I have come across the same issue and I have written a bit of code that goes through the entity framework model and removes the xml column type from the model.

Here is an example of how to use it. Just create a new wrapper DbContext class for your main code and then use that.

   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);
                }
            }
        }
    }

Hope this helps.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why