One-To-Many mapping fluent NHibernate
I am trying to get a One-To-Many relation working. I have the following mappings:
public class User
{
public User()
{
UserCourses = new List<UserCourse>();
}
public virtual int Id { get; private set; }
public virtual IList<UserCourse> UserCourses { get; private set;}
}
public sealed class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id, "Id");
HasMany(x => x.UserCourses).Inverse().Cascade.All().Table("UserCourse");
Table("[USER]");
}
}
public sealed class UserCourseMap : ClassMap<UserCourse>
{
public UserCourseMap()
{
Id(x => x.Id, "Id");
References(x => x.User, "UserID");
Map(x => x.Role, "Role");
}
}
I am getting the following exception if I try to make an instance of a User object and then tries to view the courses:
var user = (from u in userRepository.Linq() // Fetch a user
where u.Username == username
select u).Single();
var courses = user.UserCourses.Single(); // wont work
{"Invalid column name 'User_id'.\r\nInvalid column name 'User_id'."} could not initialize a collection: [Fringedivision.Rapp.Domain.User.UserCourses#1][SQL: SELECT usercourse0_.User_id as User4_1_, usercourse0_.Id as Id1_, usercourse0_.Id as Id1_0_, usercourse0_.Role as Role1_0_, usercourse0_.UserID as UserID1_0_ F开发者_开发技巧ROM [UserCourse] usercourse0_ WHERE usercourse0_.User_id=?]
I can't seem to understand what the problem is, any suggestions? The Reference mappings seems to work if I make an instance of a UserCourse object.
add the column to the has many
HasMany(x => x.UserCourses).KeyColumn("UserId")
or whatever the actualy syntax is in the version of fnh you are on
精彩评论