需要 NFLuent Hibernate 映射的帮助

发布于 2024-08-18 07:12:19 字数 4435 浏览 3 评论 0原文

我已经尽我所能寻找解决方案,但似乎我在某个地方不知所措。这是我第一次深入了解 Fluent NHibernate 和 Fluent NHibernate。 NHibernate。

简短的版本(仍然会很长)是我有一个包含表 User、Role、UserRoles 的数据库。它们之间的关系应该相当直观......一个用户可以被分配多个角色。 UserRoles 表存储这些关联,以便 User &角色不直接链接。我想我在这里需要多对多关系,但我不确定如何在 NFLuent 中映射它。

我已经在 NFLuent 入门页面上完成了示例并使其正常工作。我试图从中推测出答案,但我在某个地方遗漏了一些东西。

我将列出我的实体和一秒钟内映射,但我得到的错误是:

创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 了解更多详细信息。

潜在原因集合没有说明任何内容,但 InnerException 说明:

{“无法确定类型:DataAccess.Entities.User、DataAccess、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null,对于列:NHibernate.Mapping.Column(User)”}

hbm 文件 这是我的实体:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
public class User
    {
        public virtual int Id { get; private set; }
        public virtual string DisplayName { get; set; }
        public virtual string Email { get; set; }
        public virtual string Password { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
    public class Role
    {
        public virtual int Id { get; private set; }
        public virtual string RoleName { get; set; }
        public virtual string RoleDescription { get; set; }  
    }
}   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
    public class UserRole 
    {
         public virtual int Id { get; private set; } 
         public virtual Role Role { get; set; }
         public virtual User User { get; set; }

         //Attempted variations
         //public virtual IList<Role> Roles{ get; set; }
         //public virtual IList<User> Users { get; set; }

         //public UserRole()
         //{
         //    Roles = new List<Role>();
         //    Users = new List<User>();
         //}

         //public virtual void AddRole(Role role)
         //{
         //    Roles.Add(role);
         //}

         //public virtual void AddUser(User user)
         //{
         //    Users.Add(user);
         //}
    }
}

这是我的映射:

using DataAccess.Entities;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Mappings
{
    public class RoleMap : ClassMap<Role>
    {
        public RoleMap()
        {
            Id(x => x.Id);
            Map(x => x.RoleDescription);
            Map(x => x.RoleName);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Entities;
using FluentNHibernate.Mapping;

namespace DataAccess.Mappings
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(x => x.Id);
            Map(x => x.DisplayName);
            Map(x => x.Email);
            Map(x => x.Password);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Entities;
using FluentNHibernate.Mapping;

namespace DataAccess.Mappings
{
    public class UserRoleMap : ClassMap<UserRole>
    {
        public UserRoleMap()
        {
            Id(x => x.Id);
            Map(x => x.User);
            Map(x => x.Role);            
            //HasMany(x => x.Users)
            //  .Inverse()
            //  .Cascade.All();
            //HasMany(x => x.Roles)
            //  .Inverse()
            //  .Cascade.All();
        }
    }
}

private static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2005
            .ConnectionString(c => c
                .Server(@".\SQLEXPRESS")
                .Database("MyDB")
                .TrustedConnection())
            )                                                     
          .Mappings(m => m
            .FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
            .ExportTo(@"C:\Development\MVC\DataAccess\hbms"))
            .ExposeConfiguration((Configuration config) => new SchemaExport(config).Create(false, true))
          .BuildSessionFactory();
}   

任何建议将不胜感激。谢谢!

I've searched around for a solution as best I can, but it seems I'm at a loss somewhere. This is my first dive into Fluent NHibernate & NHibernate.

The short version (which will still be long) is that I have a database that has tables User, Role, UserRoles. The relationship b/t them should be fairly intuitive...A user can be assigned multiple roles. The UserRoles table stores these associations so User & Role don't link directly.I think I need a many-to-many relationship here, but I'm not sure how to map that in NFLuent.

I've done the example on the NFLuent getting started page and have it working. I've tried to surmise the answers from it, but I've missed something somewhere.

I will list my entity & mappings in a second, but the error I'm getting is:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

The Potential reasons collection doesn't say anything but the InnerException says:

{"Could not determine type for: DataAccess.Entities.User, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(User)"}

The hbm file
Here are my entities:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
public class User
    {
        public virtual int Id { get; private set; }
        public virtual string DisplayName { get; set; }
        public virtual string Email { get; set; }
        public virtual string Password { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
    public class Role
    {
        public virtual int Id { get; private set; }
        public virtual string RoleName { get; set; }
        public virtual string RoleDescription { get; set; }  
    }
}   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Entities
{
    public class UserRole 
    {
         public virtual int Id { get; private set; } 
         public virtual Role Role { get; set; }
         public virtual User User { get; set; }

         //Attempted variations
         //public virtual IList<Role> Roles{ get; set; }
         //public virtual IList<User> Users { get; set; }

         //public UserRole()
         //{
         //    Roles = new List<Role>();
         //    Users = new List<User>();
         //}

         //public virtual void AddRole(Role role)
         //{
         //    Roles.Add(role);
         //}

         //public virtual void AddUser(User user)
         //{
         //    Users.Add(user);
         //}
    }
}

Here are my mappings:

using DataAccess.Entities;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Mappings
{
    public class RoleMap : ClassMap<Role>
    {
        public RoleMap()
        {
            Id(x => x.Id);
            Map(x => x.RoleDescription);
            Map(x => x.RoleName);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Entities;
using FluentNHibernate.Mapping;

namespace DataAccess.Mappings
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(x => x.Id);
            Map(x => x.DisplayName);
            Map(x => x.Email);
            Map(x => x.Password);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Entities;
using FluentNHibernate.Mapping;

namespace DataAccess.Mappings
{
    public class UserRoleMap : ClassMap<UserRole>
    {
        public UserRoleMap()
        {
            Id(x => x.Id);
            Map(x => x.User);
            Map(x => x.Role);            
            //HasMany(x => x.Users)
            //  .Inverse()
            //  .Cascade.All();
            //HasMany(x => x.Roles)
            //  .Inverse()
            //  .Cascade.All();
        }
    }
}

private static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2005
            .ConnectionString(c => c
                .Server(@".\SQLEXPRESS")
                .Database("MyDB")
                .TrustedConnection())
            )                                                     
          .Mappings(m => m
            .FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
            .ExportTo(@"C:\Development\MVC\DataAccess\hbms"))
            .ExposeConfiguration((Configuration config) => new SchemaExport(config).Create(false, true))
          .BuildSessionFactory();
}   

Any suggestions would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只等公子 2024-08-25 07:12:19

您的场景类似于 Fluent NHibernate 入门 中给出的示例中的商店和产品链接。

UserRoles 只是一种链接机制,因此您最好不要将其视为一个实体。相反,您有一个已分配角色用户和/或已分配给用户角色

所以你可能有:

public class User
{
    public virtual int Id { get; private set; }
    public virtual string DisplayName { get; set; }
    public virtual string Email { get; set; }
    public virtual string Password { get; set; }
    public virtual IList<Role> Roles { get; set; }

    public User()
    {
        Roles = new List<Role>();
    }

    public AddToRole(Role role)
    {
        role.Users.Add(this);
        Roles.Add(role);
    }
}

public class Role
{
    public virtual int Id { get; private set; }
    public virtual string RoleName { get; set; }
    public virtual string RoleDescription { get; set; }
    public virtual IList<User> Users { get; set; }

    public Role()
    {
        Users = new List<User>();
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.DisplayName);
        Map(x => x.Email);
        Map(x => x.Password);
        HasManyToMany(x => x.Roles)
          .Table("UserRoles");
    }
}

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Id(x => x.Id);
        Map(x => x.RoleDescription);
        Map(x => x.RoleName);
        HasManyToMany(x => x.Users)
          .Inverse()
          .Table("UserRoles");
    }
}

Your scenario is analogous to the linking of Store and Products in the example given in the Getting started with Fluent NHibernate.

UserRoles is just a linking mechanism so you are better off not thinking of it as an entity. Instead, you have a User that has been assigned Roles and/or a Role that has been assigned to Users.

So you might have:

public class User
{
    public virtual int Id { get; private set; }
    public virtual string DisplayName { get; set; }
    public virtual string Email { get; set; }
    public virtual string Password { get; set; }
    public virtual IList<Role> Roles { get; set; }

    public User()
    {
        Roles = new List<Role>();
    }

    public AddToRole(Role role)
    {
        role.Users.Add(this);
        Roles.Add(role);
    }
}

public class Role
{
    public virtual int Id { get; private set; }
    public virtual string RoleName { get; set; }
    public virtual string RoleDescription { get; set; }
    public virtual IList<User> Users { get; set; }

    public Role()
    {
        Users = new List<User>();
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.DisplayName);
        Map(x => x.Email);
        Map(x => x.Password);
        HasManyToMany(x => x.Roles)
          .Table("UserRoles");
    }
}

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Id(x => x.Id);
        Map(x => x.RoleDescription);
        Map(x => x.RoleName);
        HasManyToMany(x => x.Users)
          .Inverse()
          .Table("UserRoles");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文