FluentNHibernate,映射关联错误

发布于 2024-11-02 22:05:04 字数 2828 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-11-09 22:05:04

我认为当您一遍又一遍地调用 .Mappings(/*mappings*/) 时,您实际上是在重置映射。

这样效果更好吗?

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();
    }

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();
    }
稀香 2024-11-09 22:05:04

我会改变您将映射添加到配置的方式。布鲁克的答案是正确的,但如果您的映射都在同一个程序集中,我会这样做:

public ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008
                       .ConnectionString(c => c.FromConnectionStringWithKey("AppData")))
                       .Mappings(m => 
                       {
                        m.FluentMappings.AddFromAssemblyOf<UserMap>()
                       })
                       //.ExposeConfiguration(BuildSchema)
                       .BuildSessionFactory();
}

这样您就不必每次创建新映射时都将新映射添加到您的配置中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文