我有一个简单的类,如下所示:
public class Item {
// some properties
public virtual IDictionary<string, Detail> Details { get; private set; }
}
然后我有一张如下所示的地图:
map.HasMany(x => x.Details).AsMap<string>("Name").AsIndexedCollection<string>("Name", c => c.GetIndexMapping()).Cascade.All().KeyColumn("Item_Id"))
有了这张地图,我收到以下错误,我不知道如何解决?
该类型或方法有 2 个泛型参数,但提供了 1 个泛型实参。必须为每个通用参数提供通用参数。
我找到了解决这个问题的方法。基本上,我阻止自动映射器尝试映射 IDictionary。它迫使我必须在覆盖中手动映射它,但至少它有效。
我正在使用从 DefaultAutomappingConfiguration 派生的 AutomappingConfiguration。
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);
}
这里有几个示例类以及我用来实现此目的的关联映射:
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();
}
}
我刚刚花了几个小时来完成这项工作,所以我希望这可以让其他人省去一个晚上的麻烦。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)