Auto mapping a IDictionary with fluent nhibernate

后端 未结 1 1881
一个人的身影
一个人的身影 2021-01-03 13:52

I have a simple class that looks like this:

public class Item {
 // some properties
public virtual IDictionary Details { get; private s         


        
相关标签:
1条回答
  • 2021-01-03 14:09

    I found a workaround for this. Basically I'm preventing the automapper from attempting to map an IDictionary. It forces me to have to map it manually in an override but at least it works.

    I'm using an AutomappingConfiguration derived from DefaultAutomappingConfiguration.

    public override bool ShouldMap(Member member)
    {
        if ( member.PropertyType.IsGenericType )
        {
            if (member.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>))
                        return false;
        }
    
        return base.ShouldMap(member);
    }
    

    And here's a couple of sample classes and the associated mapping that I'm using to make this happen:

    public class ComponentA
    {
        public virtual string Name { get; set; }
    }
    
    public class EntityF : Entity
    {
        private IDictionary<string, ComponentA> _components = new Dictionary<string, ComponentA>();
        public IDictionary<string, ComponentA> Components
        {
            get { return _components; }
            set { _components = value; }
        }
    }
    
    public class EntityFMap : IAutoMappingOverride<EntityF>
    {
        public void Override(AutoMapping<EntityF> mapping)
        {
            mapping.HasMany<ComponentA>(x => x.Components)
                .AsMap<string>("IndexKey")
                .KeyColumn("EntityF_Id")
                .Table("EntityF_Components")
                .Component(x =>
                               {
                                   x.Map(c => c.Name);
                               })
                .Cascade.AllDeleteOrphan();
        }
    }
    

    I've just spent several hours to make this work, so I hope this saves someone else an evening of hair-pulling.

    0 讨论(0)
提交回复
热议问题