使用属性来覆盖多对多的 FluentNhibernate AutoMapper
我想使用 NHibernate Automapper 来映射以下类:
public class AdminUser: Identity, IIdentityWithRoles
{
public virtual IList<Role> Roles { get; set; }
}
问题是 Automapper 在模式中创建多对一关系,其中角色具有 adminuserId。但我需要角色是多对多的。注意我无法更改 Role 类以使其包含 IList,因为它位于单独的库中并且不了解 AdminUser。
我真正想做的是能够添加一个属性:
public class AdminUser: Identity, IIdentityWithRoles
{
[ManyToMany]
public virtual IList<Role> Roles { get; set; }
}
这将强制自动映射器执行此操作。是否可以调整我的 Automapper 配置来查找此属性,或者 FluentNhibernate 中是否内置了某些功能已经可以完成此工作?
非常感谢您提供的任何帮助。
更新——
好的,感谢您的指点(已投票),但现在我被困在这里:
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
{
}
I would like to use NHibernate Automapper to map the following class:
public class AdminUser: Identity, IIdentityWithRoles
{
public virtual IList<Role> Roles { get; set; }
}
Problem is that Automapper creates a manytoone relationship within the schema where role has adminuserId on it. But I need roles to be ManyToMany with this. NOTE I cannot change the Role class to have IList on it because it is in a seperate library and has no knowledge of AdminUser.
What I really want to do is to be able to add an atrribute thus:
public class AdminUser: Identity, IIdentityWithRoles
{
[ManyToMany]
public virtual IList<Role> Roles { get; set; }
}
which will force automapper to do this. Is it possible to adjust my Automapper config to look for this attribute or is there something built into FluentNhibernate which already does this job?
Many thanks for any help you can provide.
UPDATE --
OK thanks for the pointer (upvoted) but now I am stuck here:
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
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须为默认的
AdminUser
映射编写一个覆盖:并使用您的
AutoMapping
注册它:如果这种情况不止在这一处发生,您可以将其替换为 < code>Convention,但是这有点复杂。
You have to write an override for a default
AdminUser
mapping:And register it with your
AutoMapping
:If this occurs more than in this one place, you can replace it with an
Convention
, but this is a bit more complicated.