FluentNHibernate,映射关联错误
我在使用 FluentNHibernate 的 MVC3 应用程序运行时收到以下错误,但似乎无法找出映射问题:
表 UserRole 中的关联引用了未映射的类:Domain.Entities.User 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:NHibernate.MappingException:表 UserRole 中的关联引用未映射的类:Domain.Entities.User
源错误: 公共 ISessionFactory CreateSessionFactory() { 返回 Fluently.Configure() .数据库(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
两个相关的类和映射:看不出这里有什么问题?!
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);
}
}
有什么想法吗?
添加我的会话工厂:
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为当您一遍又一遍地调用
.Mappings(/*mappings*/)
时,您实际上是在重置映射。这样效果更好吗?
I think when you call
.Mappings(/*mappings*/)
over and over again you're essentially resetting the mappings.Does this work better?
我会改变您将映射添加到配置的方式。布鲁克的答案是正确的,但如果您的映射都在同一个程序集中,我会这样做:
这样您就不必每次创建新映射时都将新映射添加到您的配置中。
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:
This way you don't have to add the new mapping to your config every time you create a new mapping.