使用属性来覆盖多对多的 FluentNhibernate AutoMapper

发布于 2024-10-31 02:58:57 字数 1056 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄暮涼年 2024-11-07 02:58:58

您必须为默认的 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>()))

如果这种情况不止在这一处发生,您可以将其替换为 < code>Convention,但是这有点复杂

You have to write an override for a default AdminUser mapping:

public class AdminUserOverride : IAutoMappingOverride<AdminUser>
{
  public void Override(AutoMapping<AdminUser> mapping)
  {
    mapping.HasManyToMany(x => x.Roles); // and possibly other options here
  }
}

And register it with your AutoMapping:

Fluently.Configure()
  .Database(/* database config */)
  .Mappings(m =>
    m.AutoMappings
      .Add(AutoMap.AssemblyOf<AdminUser>().UseOverridesFromAssemblyOf<AdminUser>()))

If this occurs more than in this one place, you can replace it with an Convention, but this is a bit more complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文