NHibernate Could not determine type for X

Gunnar picture Gunnar · Aug 28, 2013 · Viewed 10.1k times · Source

I have created a Custom Membership Provider using NHibernate for my MVC 3 ASP.NET project.

I have some problems with the User class. I get the following error:

Could not determine type for: FluentNHibernateMembershipProvider.Models.User, FluentNHibernateMembershipProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(User)

The error occurs when I call:

_sessionFactory = cfg.BuildSessionFactory();

The User.cs file looks like this:

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

    }
}

My UserMappings.cs file looks like this:

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

--- UPDATED ---

Here are the two other mapped classes:

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

RoleMappings.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);
        }
    }
}

--- UPDATED FINAL SOLUTION ---

I had to change the OAuthToken and OAuthTokenMappings slightly, because the Composite key was two string variables, which is not allowed, so this was my final solution:

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

Answer

JTMon picture JTMon · Aug 28, 2013

I think your problems comes from this mapping:

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