1

I'm trying to map a many-to-many relation, but the mapping doesn't work (list property stays null and items are not stored.

I use the following tables:

User(Id, Name, Password,...)
Role(Id, Name)
RoleUser(UserId, RoleId) with a compound primary key

My entity files are

public class User
{
    public virtual int Id { get; protected set; }
    public virtual string UserName { get; set; }
    public virtual IList<Role> Roles { get; set; }
}
public class Role
{
    public virtual int Id { get; protected set; }
    public virtual IList<User> Users { get; set; }
    public virtual string Name { get; set; }
}

There are two conventions that are applied:

public class MyForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        return property == null ? type.Name + "Id" : property.Name + "Id";
    }
}
public class MyManyToManyConvention : IHasManyToManyConvention
{
    public void Apply(IManyToManyCollectionInstance instance)
    {
        var firstName = instance.EntityType.Name;
        var secondName = instance.ChildType.Name;

        if (StringComparer.OrdinalIgnoreCase.Compare(firstName, secondName) > 0)
        {
            instance.Table(string.Format("{0}{1}", secondName, firstName));
            instance.Inverse();
        }
        else
        {
            instance.Table(string.Format("{0}{1}", firstName, secondName));
            instance.Not.Inverse();
        }

        instance.Cascade.All();
    }
}

When I export the mappings to files, i get the following snippet within user definition as an example:

<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="Id" />
  <generator class="identity" />
</id>
<bag cascade="all" inverse="true" name="Roles" table="RoleUser">
  <key>
    <column name="UserId" />
    <column name="UserId" />
  </key>
  <many-to-many class="Security.Role, Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt;
    <column name="RoleId" />
  </many-to-many>
</bag>
<property name="UserName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="UserName" />
</property>

The duplicate column UserId within the key seems not correct to me. I'm not sure what I made wrong. Thanks for help.

Martin H.
  • 1,086
  • 1
  • 17
  • 28
  • does schemaexport generate the right schema? you can export the ddl into a file instead of the db – Firo May 13 '13 at 10:03
  • The schema export snippet was generated by method ExportTo(folder). What do you mean? – Martin H. May 13 '13 at 13:57
  • you posted the generated mappings but i would like to see the `new SchemExport(config).Execute(new StreamWriter("somefile.txt").WriteLine, false, null, false)` – Firo May 13 '13 at 20:22
  • Thanks Firo. This hint is very useful. The problem was the schema of the mapping table. I ended up with a custom ManyToManyTableConvention. – Martin H. May 23 '13 at 09:48

1 Answers1

-1

The problem was the schema of the mapping table. I ended up with a custom ManyToManyTableConvention.

Martin H.
  • 1,086
  • 1
  • 17
  • 28