Fluent NHibernate question

后端 未结 4 2139
难免孤独
难免孤独 2021-01-05 04:00

Let\'s say you have two tables, \"Users\" and \"UserRoles\". Here\'s how the two tables are structured (table - columns):

Users - UserID (int)

UserRoles - U

相关标签:
4条回答
  • 2021-01-05 04:28

    What you're looking for is a of a set of elements, which in standard hbm mapping is:

    <set name="Roles" table="UserRoles">
      <key column="UserID" />
      <element column="Role" />
    </set>
    

    For Fluent NHibernate you can map this like so:

    HasMany<string>(x => x.Roles)
      .AsElement("Role");
    

    You may need to also specify the key name using WithKeyColumn(string).

    0 讨论(0)
  • 2021-01-05 04:30

    This also worked:

    HasMany<Role>(u => u.Roles)
                    .WithTableName("UserRoles")
                    .Component(role => role.Map(r => r.Name))
                    .AsList();
    

    You don't need to map Role or UserRoles.

    Make sure Role implements IEquatable < Role > ;.

    0 讨论(0)
  • 2021-01-05 04:33

    FWIW this has change minorly of present day. The current mapping is

    HasMany<string>(x => x.Roles)
      .Element("Role");
    
    0 讨论(0)
  • 2021-01-05 04:39

    I beleive it would be

    public User()
      {
        Id(x => x.UserID);
        HasMany<UserRoles>(x => x.UserRoles).AsBag();
      }
    

    You'll also have to make sure you map your UserRoles class as well

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