Reputation: 1082
I am getting the following error at runtime for my MVC3 application with FluentNHibernate but can't seem to figure out the mapping issue:
An association from the table UserRole refers to an unmapped class: Domain.Entities.User Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: NHibernate.MappingException: An association from the table UserRole refers to an unmapped class: Domain.Entities.User
Source Error: public ISessionFactory CreateSessionFactory() { return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
The two relevant classes and maps: Can't see what the problem is here?!
public class User : IUser
{
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual Guid UserGUID { get; set; }
public virtual int FileQuota { get; set; }
public virtual Company Company { get; set; }
public virtual IList<UserRole> UserRoles { get; set; }
public virtual IList<CloudFile> CloudFiles { get; set; }
}
public class UserRole : IUserRole
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual User User { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.UserName);
Map(x => x.FirstName);
Map(x => x.Password);
Map(x => x.LastName);
Map(x => x.Email);
Map(x => x.UserGUID);
Map(x => x.FileQuota);
References(x => x.Company);
HasMany(x => x.UserRoles).Cascade.All();
HasMany(x => x.CloudFiles).Cascade.All();
}
}
public class UserRoleMap : ClassMap<UserRole>
{
public UserRoleMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
References(x => x.User);
}
}
Any ideas??
Adding my session factory:
public ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
.Mappings(m => m.FluentMappings.Add(typeof(Domain.Mappings.UserMap)))
.Mappings(m => m.FluentMappings.Add(typeof(Domain.Mappings.CloudFileMap)))
.Mappings(m => m.FluentMappings.Add(typeof(Domain.Mappings.CompanyMap)))
.Mappings(m => m.FluentMappings.Add(typeof(Domain.Mappings.UserRoleMap)))
//.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
Upvotes: 0
Views: 419
Reputation: 15293
I would change the way you are adding your mappings to your config. Brook's answer is correct but if your mappings are all in the same assembly I would do something like this:
public ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<UserMap>()
})
//.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
This way you don't have to add the new mapping to your config every time you create a new mapping.
Upvotes: 1
Reputation: 6009
I think when you call .Mappings(/*mappings*/)
over and over again you're essentially resetting the mappings.
Does this work better?
public ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
.Mappings(m =>
{
m.FluentMappings.Add(typeof(Domain.Mappings.UserMap));
m.FluentMappings.Add(typeof(Domain.Mappings.CloudFileMap));
m.FluentMappings.Add(typeof(Domain.Mappings.CompanyMap));
m.FluentMappings.Add(typeof(Domain.Mappings.UserRoleMap));
})
//.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
Upvotes: 3