HasMany should be HasManyToMany
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!
Support Staff 2 Posted by Paul Batum on 07 Jul, 2010 11:37 AM
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);
}}
3 Posted by Jacob Madsen on 07 Jul, 2010 04:23 PM
Thanks Paul. I really appreciate your help!
I can use your suggestion in most cases, but what is the syntax for a similar convention?
Support Staff 4 Posted by Paul Batum on 07 Jul, 2010 10:19 PM
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.