Nhibernate 错误“列名无效”当列确实存在时

发布于 2024-11-29 02:46:55 字数 6247 浏览 1 评论 0原文

您好,我有一个名为 document 的对象和一个名为 user

Document

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual User User { get; set; }

Documentmap

    public DocumentMap()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.User);
    }

User 的对象,

    public virtual string UserId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleInitial { get; set; }
    public virtual string LastName { get; set; }
    private readonly IList<Document> _documents = new List<Document>();
    public virtual IEnumerable<Document> Documents { get { return _documents; } }
    public virtual void Remove(Document document)
    {
        _documents.Remove(document);
    }
    public virtual void Add(Document document)
    {
        if (!document.IsNew() && _documents.Contains(document)) return;
        _documents.Add(document);
    }

    Map(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.MiddleInitial);
        Map(x => x.LastName);
        HasMany(x => x.Documents).Access.CamelCaseField(Prefix.Underscore); 

非常简单(它们从基类继承,具有诸如createddate、modifieddate 等内容),

当我尝试通过 userid 获取所有文档时,我得到这个

无效的列名“UserId”。

该列绝对在表中。它还列出了一些不存在的基类项目。

我获取 sql 并将其传递到查询管理器中,我得到智能感知,说它们是无效列。我运行它并且执行得很好。此外,还有许多其他对象使用这些基类,没有任何问题。

我尝试了各种方法,例如显式映射键名称、使用逆函数的列名称等,但均无济于事。真的不知道该怎么办。 谢谢, Raif

//根据要求编辑抱歉,它太冗长了。数据库是由 nhibernate 创建模式

文档

public class Document : Entity
{
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual DocumentCategory DocumentCategory { get; set; }
    [ValueOf(typeof(DocumentFileType))]
    public virtual string FileType { get; set; }
    public virtual string FileUrl { get; set; }
    public virtual int? Pages { get; set; }
    public virtual decimal? Size { get; set; }
    public virtual User User { get; set; }
}

public class DocumentMap : EntityMap<Document>
{
    public DocumentMap()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        Map(x => x.FileUrl);
        Map(x => x.Pages);
        Map(x => x.Size);
        Map(x => x.FileType);
        References(x => x.DocumentCategory);
        References(x => x.User);
    }
}

实体

public class Entity : IGridEnabledClass, IEquatable<Entity>
{
    public virtual int EntityId { get; set; }
    public virtual DateTime? CreateDate { get; set; }
    public virtual DateTime? ChangeDate { get; set; }
    public virtual int ChangedBy { get; set; }
    public virtual bool Archived { get; set; }

    public virtual bool IsNew()
    {
        return EntityId == 0;
    }

用户

 public class User : DomainEntity, IUser
{
    public virtual string UserId { get; set; }
    [ValidateNonEmpty]
    public virtual string FirstName { get; set; }
    public virtual string MiddleInitial { get; set; }
    [ValidateNonEmpty]
    public virtual string LastName { get; set; }
    public virtual string Title { get; set; }
    public virtual DateTime? BirthDate { get; set; }
    public virtual string StartPage { get; set; }
    public virtual UserLoginInfo UserLoginInfo { get; set; }
    public virtual UserStatus UserStatus { get; set; }
    public virtual Photo HeadShot { get; set; }
    private readonly IList<Document> _documents = new List<Document>();
    public virtual IEnumerable<Document> Documents { get { return _documents; } }
    public virtual void Remove(Document document)
    {
        _documents.Remove(document);
    }
    public virtual void Add(Document document)
    {
        if (!document.IsNew() && _documents.Contains(document)) return;
        _documents.Add(document);
    }
    several more collections

public class UserMap : DomainEntityMap<User>
{
    public UserMap()
    {
        Map(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.MiddleInitial);
        Map(x => x.LastName);
        Map(x => x.BirthDate);
        Map(x => x.StartPage);
        References(x => x.UserStatus);
        References(x => x.UserLoginInfo);
        References(x => x.HeadShot);
        HasMany(x => x.Documents).Access.CamelCaseField(Prefix.Underscore); 

数据库表创建的,从脚本中选择到

  SELECT [EntityId]
  ,[CreateDate]
  ,[ChangeDate]
  ,[ChangedBy]
  ,[Archived]
  ,[Name]
  ,[Description]
  ,[FileUrl]
  ,[Pages]
  ,[Size]
  ,[FileType]
  ,[DocumentCategoryId]
  ,[UserId]
  FROM [DecisionCriticalSuite].[dbo].[Document]
 GO


  SELECT [EntityId]
  ,[CreateDate]
  ,[ChangeDate]
  ,[ChangedBy]
  ,[Archived]
  ,[TenantId]
  ,[OrgId]
  ,[UserId]
  ,[FirstName]
  ,[MiddleInitial]
  ,[LastName]
  ,[BirthDate]
  ,[StartPage]
  ,[UserStatusId]
  ,[UserLoginInfoId]
  ,[HeadShotId]
  ,[OrganizationId]
  FROM [DecisionCriticalSuite].[dbo].[User]
  GO

来自 nhprof 的 Management Studio 上的菜单项错误

ERROR: 
Invalid column name 'UserId'.
Invalid column name 'UserId'.
Invalid column name 'EntityId'.
Invalid column name 'EntityId'.
Invalid column name 'CreateDate'.
Invalid column name 'ChangeDate'.
Invalid column name 'ChangedBy'.
Invalid column name 'Archived'.
Invalid column name 'FileType'.
Invalid column name 'UserId'.Could not execute query: SELECT documents0_.UserId as UserId1_, documents0_.EntityId as EntityId1_, documents0_.EntityId as EntityId49_0_, documents0_.CreateDate as CreateDate49_0_, documents0_.ChangeDate as ChangeDate49_0_, documents0_.ChangedBy as ChangedBy49_0_, documents0_.Archived as Archived49_0_, documents0_.Name as Name49_0_, documents0_.Description as Descript7_49_0_, documents0_.FileUrl as FileUrl49_0_, documents0_.Pages as Pages49_0_, documents0_.Size as Size49_0_, documents0_.FileType as FileType49_0_, documents0_.DocumentCategoryId as Documen12_49_0_, documents0_.UserId as UserId49_0_ FROM [Document] documents0_ WHERE           documents0_.UserId=@p0

Hi I have an object called document and one called user

Document

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual User User { get; set; }

Documentmap

    public DocumentMap()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.User);
    }

User

    public virtual string UserId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleInitial { get; set; }
    public virtual string LastName { get; set; }
    private readonly IList<Document> _documents = new List<Document>();
    public virtual IEnumerable<Document> Documents { get { return _documents; } }
    public virtual void Remove(Document document)
    {
        _documents.Remove(document);
    }
    public virtual void Add(Document document)
    {
        if (!document.IsNew() && _documents.Contains(document)) return;
        _documents.Add(document);
    }

    Map(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.MiddleInitial);
        Map(x => x.LastName);
        HasMany(x => x.Documents).Access.CamelCaseField(Prefix.Underscore); 

Pretty straighforward ( they inherit from base class with stuff like createddate modifieddate etc )

when I try to get all doc by userid I get this

Invalid column name 'UserId'.

the column most definitely is in the table. It also lists several of the base class items as not being there.

I take the sql and past it in query manager and I get intellisense saying they are invalid columns. I run it and it executes just fine. Further more there are plenty of other objects using these base classes with no problems.

I have tried various things like explicitly mapping the key name, the column name using inverse etc to no avail. Don't really know what to do.
Thanks,
Raif

//EDIT as per request sorry it's so verbose. the database is created by nhibernate create schema

Document

public class Document : Entity
{
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual DocumentCategory DocumentCategory { get; set; }
    [ValueOf(typeof(DocumentFileType))]
    public virtual string FileType { get; set; }
    public virtual string FileUrl { get; set; }
    public virtual int? Pages { get; set; }
    public virtual decimal? Size { get; set; }
    public virtual User User { get; set; }
}

public class DocumentMap : EntityMap<Document>
{
    public DocumentMap()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        Map(x => x.FileUrl);
        Map(x => x.Pages);
        Map(x => x.Size);
        Map(x => x.FileType);
        References(x => x.DocumentCategory);
        References(x => x.User);
    }
}

Entity

public class Entity : IGridEnabledClass, IEquatable<Entity>
{
    public virtual int EntityId { get; set; }
    public virtual DateTime? CreateDate { get; set; }
    public virtual DateTime? ChangeDate { get; set; }
    public virtual int ChangedBy { get; set; }
    public virtual bool Archived { get; set; }

    public virtual bool IsNew()
    {
        return EntityId == 0;
    }

User

 public class User : DomainEntity, IUser
{
    public virtual string UserId { get; set; }
    [ValidateNonEmpty]
    public virtual string FirstName { get; set; }
    public virtual string MiddleInitial { get; set; }
    [ValidateNonEmpty]
    public virtual string LastName { get; set; }
    public virtual string Title { get; set; }
    public virtual DateTime? BirthDate { get; set; }
    public virtual string StartPage { get; set; }
    public virtual UserLoginInfo UserLoginInfo { get; set; }
    public virtual UserStatus UserStatus { get; set; }
    public virtual Photo HeadShot { get; set; }
    private readonly IList<Document> _documents = new List<Document>();
    public virtual IEnumerable<Document> Documents { get { return _documents; } }
    public virtual void Remove(Document document)
    {
        _documents.Remove(document);
    }
    public virtual void Add(Document document)
    {
        if (!document.IsNew() && _documents.Contains(document)) return;
        _documents.Add(document);
    }
    several more collections

public class UserMap : DomainEntityMap<User>
{
    public UserMap()
    {
        Map(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.MiddleInitial);
        Map(x => x.LastName);
        Map(x => x.BirthDate);
        Map(x => x.StartPage);
        References(x => x.UserStatus);
        References(x => x.UserLoginInfo);
        References(x => x.HeadShot);
        HasMany(x => x.Documents).Access.CamelCaseField(Prefix.Underscore); 

database tables create from script select to menu item on management studio

  SELECT [EntityId]
  ,[CreateDate]
  ,[ChangeDate]
  ,[ChangedBy]
  ,[Archived]
  ,[Name]
  ,[Description]
  ,[FileUrl]
  ,[Pages]
  ,[Size]
  ,[FileType]
  ,[DocumentCategoryId]
  ,[UserId]
  FROM [DecisionCriticalSuite].[dbo].[Document]
 GO


  SELECT [EntityId]
  ,[CreateDate]
  ,[ChangeDate]
  ,[ChangedBy]
  ,[Archived]
  ,[TenantId]
  ,[OrgId]
  ,[UserId]
  ,[FirstName]
  ,[MiddleInitial]
  ,[LastName]
  ,[BirthDate]
  ,[StartPage]
  ,[UserStatusId]
  ,[UserLoginInfoId]
  ,[HeadShotId]
  ,[OrganizationId]
  FROM [DecisionCriticalSuite].[dbo].[User]
  GO

error from nhprof

ERROR: 
Invalid column name 'UserId'.
Invalid column name 'UserId'.
Invalid column name 'EntityId'.
Invalid column name 'EntityId'.
Invalid column name 'CreateDate'.
Invalid column name 'ChangeDate'.
Invalid column name 'ChangedBy'.
Invalid column name 'Archived'.
Invalid column name 'FileType'.
Invalid column name 'UserId'.Could not execute query: SELECT documents0_.UserId as UserId1_, documents0_.EntityId as EntityId1_, documents0_.EntityId as EntityId49_0_, documents0_.CreateDate as CreateDate49_0_, documents0_.ChangeDate as ChangeDate49_0_, documents0_.ChangedBy as ChangedBy49_0_, documents0_.Archived as Archived49_0_, documents0_.Name as Name49_0_, documents0_.Description as Descript7_49_0_, documents0_.FileUrl as FileUrl49_0_, documents0_.Pages as Pages49_0_, documents0_.Size as Size49_0_, documents0_.FileType as FileType49_0_, documents0_.DocumentCategoryId as Documen12_49_0_, documents0_.UserId as UserId49_0_ FROM [Document] documents0_ WHERE           documents0_.UserId=@p0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2024-12-06 02:46:55

确保 nhibernate 查询的数据库与您在 sql Management Studio 中查询的数据库相同。

Make sure nhibernate is querying against the same database as what you are querying against in sql management studio.

权谋诡计 2024-12-06 02:46:55

我遇到了同样的问题,因为我已将 Entity1.Entity2 映射为 Entity3。

因此,在加入时,它将尝试使用 Entity3 中的属性,就像它存在于 Entity2 上一样。

I just had the same issue because I had mapped Entity1.Entity2 as Entity3.

So when joining, it would attempt to use a property from Entity3 as if it existed on Entity2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文