我已经为我的MVC 3 ASP.NET项目使用NHibernate创建了一个自定义的Membership Provider。但是在User类上遇到了一些问题,出现以下错误:
“无法确定类型: FluentNHibernateMembershipProvider.Models.User, FluentNHibernateMembershipProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(User)”
当我调用时会出现这个错误。请帮忙解决。
用户.cs文件如下所示:
“无法确定类型: FluentNHibernateMembershipProvider.Models.User, FluentNHibernateMembershipProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(User)”
当我调用时会出现这个错误。请帮忙解决。
_sessionFactory = cfg.BuildSessionFactory();
用户.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");
}
}
}
--- 更新 ---
这里是另外两个映射类:
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);
}
}
}
---更新的最终解决方案---
我不得不稍微更改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);
}
}