gpt4 book ai didi

c# - NHibernate 无法确定 X 的类型

转载 作者:太空狗 更新时间:2023-10-29 21:01:55 25 4
gpt4 key购买 nike

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

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

Could not determine type for: FluentNHibernateMembershipProvider.Models.User, FluentNHibernateMembershipProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: 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,因为 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);
}
}

最佳答案

我认为你的问题来自这个映射:

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

关于c# - NHibernate 无法确定 X 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com