我想使用NHibernate Automapper来映射以下类:
public class AdminUser: Identity, IIdentityWithRoles
{
public virtual IList<Role> Roles { get; set; }
}
问题是Automapper在架构中创建了多个关系,其中角色具有adminuserId。但我需要扮演ManyToMany角色。注意我无法更改Role类以使其具有IList,因为它位于单独的库中且不了解AdminUser。
我真正想做的是能够添加一个属性:
public class AdminUser: Identity, IIdentityWithRoles
{
[ManyToMany]
public virtual IList<Role> Roles { get; set; }
}
将迫使automapper执行此操作。是否可以调整我的Automapper配置以查找此属性,或者FluentNhibernate内置了哪些内容已经完成此任务?
非常感谢您提供的任何帮助。
更新 -
好的感谢指针(upvoted),但现在我被困在这里:
public class ManyToManyConvention : AttributePropertyConvention<ManyToManyAttribute>
{
protected override void Apply(ManyToManyAttribute attribute, IPropertyInstance instance)
{
How can I now say that this property should be a many to may relationship
}
}
public class ManyToManyAttribute : Attribute
{
}
答案 0 :(得分:3)
您必须为默认AdminUser
映射编写覆盖:
public class AdminUserOverride : IAutoMappingOverride<AdminUser>
{
public void Override(AutoMapping<AdminUser> mapping)
{
mapping.HasManyToMany(x => x.Roles); // and possibly other options here
}
}
并将其注册到您的AutoMapping
:
Fluently.Configure()
.Database(/* database config */)
.Mappings(m =>
m.AutoMappings
.Add(AutoMap.AssemblyOf<AdminUser>().UseOverridesFromAssemblyOf<AdminUser>()))
如果这种情况多发生在这个地方,您可以将其替换为Convention
,但this is a bit more complicated。