HasMany should be HasManyToMany

Jacob Madsen's Avatar

Jacob Madsen

06 Jul, 2010 04:25 PM via web

Hi there,

I have the followed types:

public class User {

public virtual int Id {get;set;}

}

public class Group {

public virtual int Id {get;set;}
public virtual IList<User> Users {get; private set;}
public Group() {
    Users = new List<User>();
}

}

The automapper creates the schema: Users { Id, GroupId } and Groups { Id }

This is wrong in my application, since a User can be in several groups.

Right now I have the following convention that is executed:
public class DefaultHasManyConvention : IHasManyConvention, IHasManyConventionAcceptance {

public void Accept(IAcceptanceCriteria<IOneToManyCollectionInspector> criteria)
{
    criteria.Expect(all => true);
}

public void Apply(IOneToManyCollectionInstance instance)
{
}

}

I think a solution would be to change the HasMany to HasManyToMany.

Is this possible and a correct way to handle this issue?

Cheers!

  1. Support Staff 2 Posted by Paul Batum on 07 Jul, 2010 11:37 AM

    Paul Batum's Avatar

    Instead of using a convention, using an automapping override (conventions
    are for rules to be applied in broad situations - in this case you just want
    to make the automapper understand that its a many-to-many relationship).

    http://wiki.fluentnhibernate.org/Auto_mapping#Overrides

    So something like:

    public class GroupMappingOverride
      : IAutoMappingOverride<Group>{
      public void Override(AutoMapping<Group> mapping)
      { group.HasManyToMany(x => x.Users);

      }}

  2. 3 Posted by Jacob Madsen on 07 Jul, 2010 04:23 PM

    Jacob Madsen's Avatar

    Thanks Paul. I really appreciate your help!

    I can use your suggestion in most cases, but what is the syntax for a similar convention?

  3. Support Staff 4 Posted by Paul Batum on 07 Jul, 2010 10:19 PM

    Paul Batum's Avatar

    You can't create a convention that will change one-to-many relationships
    into many-to-many relationships.

    Sometimes the automapper will recognise your many to many relationships and
    you won't need to provide an override. Unfortunately I'm not familiar with
    the details of exactly when that works and when it does not. James might
    know.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Ten divided by two is what?

     

    08 Aug, 2010 08:12 AM
    08 Aug, 2010 01:39 AM
    07 Aug, 2010 12:12 PM
    07 Aug, 2010 11:51 AM
    07 Aug, 2010 12:32 AM