Many to Many Collection in both Entities Fluent NHibernate
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: 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.
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");
精彩评论