views:

39

answers:

1

I'm trying to map a manyToMany relationship in Fluent NHibernate and running into a problem that's most likely just me being new at the tool.

I have 2 Entities, User and Project. A User can be on many projects and a project can have many users.

In my map for User I have

     HasManyToMany(x => x.Projects).Inverse();

When i put the identical map in project i get an exception because the table name is opposite. Also i thought I didnt need it based on this post: http://stackoverflow.com/questions/108396/fluent-nhibernate-many-to-many

I'm stepping through this to see if it is working:

     var user = _userRepository.FindByUserName("Josh");
     var projects = user.Projects;
     var user2 = projects[0].Users;

What happens is projects returns a collection containing my project. User2 is null though. I would expect user2 to be a collection containing the same user as user.

So what am i doing wrong. Thanks.

+2  A: 

Try putting .Inverse() on one mapping only.

UserMap:

HasManyToMany(x => x.Projects).Inverse();

ProjectMap:

HasManyToMany(x => x.Users);

If that doens't work try specifing the table name.

UserMap:

HasManyToMany(x => x.Projects).Inverse().Table("ProjectUser");

ProjectMap:

HasManyToMany(x => x.Users).Table("ProjectUser");
Catalin DICU
I was able to make this work by doing both Inverse() and Table on one of the mappings. Oddly Fluent assumed the table name, non inverted, to be ProjectToUser but inverted assumed ProjectsToUsers (plural). Not sure why that is different, seems like a bug. Thanks
JoshReedSchramm