7

我使用 NHibernate 为我的 MVC 3 ASP.NET 项目创建了一个自定义成员资格提供程序。

我对 User 类有一些问题。我收到以下错误:

无法确定类型:FluentNHibernateMembershipProvider.Models.User、FluentNHibernateMembershipProvider、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null,对于列:NHibernate.Mapping.Column(User)

当我打电话时发生错误:

_sessionFactory = cfg.BuildSessionFactory();

User.cs 文件如下所示:

namespace FluentNHibernateMembershipProvider.Models
{
    public class User
    {
        public virtual int UserId { get; protected set; }
        public virtual string Username { get; set; }
        public virtual DateTime CreateDate { get; set; }
        public virtual string ConfirmationToken { get; set; }
        public virtual bool IsConfirmed { get; set; }
        public virtual DateTime LastPasswordFailureDate { get; set; }
        public virtual int PasswordFailuresSinceLastSuccess { get; set; }
        public virtual string Password { get; set; }
        public virtual DateTime PasswordChangeDate { get; set; }
        public virtual string PasswordSalt { get; set; }
        public virtual string PasswordVerificationToken { get; set; }
        public virtual DateTime PasswordVerificationTokenExpirationDate { get; set; }
        public virtual IList<Role> Roles { get; set; }

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

    }
}

我的 UserMappings.cs 文件如下所示:

namespace FluentNHibernateMembershipProvider.Mappings
{
    public class UserMappings : ClassMap<User>
    {
        public UserMappings()
        {
            Id(c => c.UserId);
            Map(c => c.Username).Unique();
            Map(c => c.CreateDate).Nullable();
            Map(c => c.ConfirmationToken).Nullable();
            Map(c => c.IsConfirmed).Default("0");
            Map(c => c.LastPasswordFailureDate).Nullable();
            Map(c => c.PasswordFailuresSinceLastSuccess).Default("0");
            Map(c => c.Password);
            Map(c => c.PasswordChangeDate).Nullable();
            Map(c => c.PasswordSalt);
            Map(c => c.PasswordVerificationToken);
            Map(c => c.PasswordVerificationTokenExpirationDate);
            HasManyToMany<Role>(c => c.Roles).Cascade.All().Inverse().Table("UsersInRole");
        }
    }
}

- - 更新 - -

以下是另外两个映射类:

角色.cs

namespace FluentNHibernateMembershipProvider.Models
{
    public class Role
    {
        public virtual int RoleId { get; protected set; }
        public virtual string RoleName { get; set; }
        public virtual IList<User> Users { get; set; }

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

角色映射.cs:

namespace FluentNHibernateMembershipProvider.Mappings
{
    public class RoleMappings : ClassMap<Role>
    {
        public RoleMappings()
        {
            Id(c => c.RoleId);
            Map(c => c.RoleName);
            HasManyToMany<User>(c => c.Users).Cascade.All().Table("UsersInRole");
        }
    }
}

OAuthToken.cs:

namespace FluentNHibernateMembershipProvider.Models
{
    public class OAuthToken
    {
        public virtual string Provider { get; set; }
        public virtual string ProviderUserId { get; set; }
        public virtual User User { get; set; }
        public virtual string Token { get; set; }
        public virtual string Secret { get; set; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(OAuthToken)) return false;
            return Equals((OAuthToken)obj);
        }

        public virtual bool Equals(OAuthToken other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Provider == Provider && other.ProviderUserId == ProviderUserId;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Provider.GetHashCode() * 397) ^ ProviderUserId.GetHashCode();
            }
        }
    }
}

OAuthTokenMappings.cs:

namespace FluentNHibernateMembershipProvider.Mappings
{
    public class OAuthTokenMappings : ClassMap<OAuthToken>
    {
        public OAuthTokenMappings()
        {
            CompositeId()
                .KeyReference(c => c.Provider, "Provider")
                .KeyReference(c => c.ProviderUserId, "ProviderUserId");
            Map(c => c.Token);
            Map(c => c.User);
            Map(c => c.Secret);
        }
    }
}

--- 更新的最终解决方案 ---

我不得不稍微更改 OAuthToken 和 OAuthTokenMappings,因为复合键是两个字符串变量,这是不允许的,所以这是我的最终解决方案:

OAuthToken.cs:

public class OAuthToken
{

    public virtual int Id { get; protected set; }
    public virtual string Provider { get; set; }
    public virtual string ProviderUserId { get; set; }
    public virtual User User { get; set; }
    public virtual string Token { get; set; }
    public virtual string Secret { get; set; }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof(OAuthToken)) return false;
        return Equals((OAuthToken)obj);
    }

    public virtual bool Equals(OAuthToken other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other.Provider == Provider && other.ProviderUserId == ProviderUserId;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (Provider.GetHashCode() * 397) ^ ProviderUserId.GetHashCode();
        }
    }
}

OAuthTokenMappings.cs:

public class OAuthTokenMappings : ClassMap<OAuthToken>
{
    public OAuthTokenMappings()
    {
        Id(c => c.Id);
        Map(c => c.Provider).UniqueKey("OAuthTokenUniqueKey");
        Map(c => c.ProviderUserId).UniqueKey("OAuthTokenUniqueKey");
        Map(c => c.Token);
        HasOne(c => c.User);
        Map(c => c.Secret);
    }
}
4

1 回答 1

14

我认为您的问题来自此映射:

public OAuthTokenMappings()
    {
        CompositeId()
            .KeyReference(c => c.Provider, "Provider")
            .KeyReference(c => c.ProviderUserId, "ProviderUserId");
        Map(c => c.Token);
        Map(c => c.User); //This is most likely the culprit. Should be a References or HasOne mapping
        Map(c => c.Secret);
    }
于 2013-08-28T12:52:15.753 回答