我已经使用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");
}
}
}
---更新---
以下是另外两个映射类:
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,因为Composite键是两个字符串变量,这是不允许的,所以这是我的最终解决方案:
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);
}
}
答案 0 :(得分:12)
我认为你的问题来自这种映射:
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);
}