gpt4 book ai didi

fluent-nhibernate - NHibernate 异常 : could not initialize a collection, 无效的列名。流畅的映射。也许是多对一的问题?

转载 作者:行者123 更新时间:2023-12-04 07:42:35 25 4
gpt4 key购买 nike

我对通过 NHibernate 遇到的异常感到困惑和沮丧。对于这篇文章的长度,我深表歉意,但我已尝试包含适当的详细信息来解释问题,以便获得一些帮助!

以下是事实:

  • 我有一个 Person包含属性 BillingManager 的类,这也是一个 Person类型。我将其映射为 FNH“引用”。
  • 我有一个 ExpenseReport包含属性 SubmittedBy 的类,这是一个 Person类型。我将其映射为 FNH“引用”。
  • 我有一个 BillableTime包含属性 Person 的类,这是一个 Person类型。我将其映射为 FNH“引用”。
  • Person包含 ExpenseReport 的集合 (IList)类型(属性 ExpenseReports)
  • Person包含 BilledTime 的集合 (IList)类型(属性 Time)

  • (请参阅帖子底部的类和映射。)

    一切都很酷,直到我添加了 IList<BilledTime> Time收藏到 Person .现在,当我尝试访问 _person.Time 时,我得到一个异常(exception):

    编码:
    // Get billable hours
    if (_person.Time == null ||
    _person.Time.Count(x => x.Project.ProjectId == project.ProjectId) == 0)
    {
    // No billable time for this project
    billableHours = Enumerable.Repeat(0F, 14).ToArray();
    }

    异常(exception):
    could not initialize a collection: 
    [MyApp.Business.Person.Time#211d3567-6e20-4220-a15c-74f8784fe47a]
    [SQL: SELECT
    time0_.BillingManager_id as BillingM8_1_,
    time0_.Id as Id1_,
    time0_.Id as Id1_0_,
    time0_.ReadOnly as ReadOnly1_0_,
    time0_.DailyHours as DailyHours1_0_,
    time0_.Week_id as Week4_1_0_,
    time0_.Person_id as Person5_1_0_,
    time0_.Project_id as Project6_1_0_,
    time0_.Invoice_id as Invoice7_1_0_
    FROM [BillableTime] time0_
    WHERE time0_.BillingManager_id=?]

    确实如此 BillingManager_id是一个无效的列名,它不存在于 BillableTime table 。但是,我不明白为什么 NHB 创建了这个 SQL……对我来说没有意义。在搜索解决方案时,我经常看到这个“无效的列名”异常,但似乎没有任何效果。更令人困惑的是:像 BilledTime , ExpenseReport type 还包含对 Person 的引用它完美无缺。

    我能够弄清楚的一件事是,如果我从 Person 映射( References(p => p.BillingManager) )中删除 BillingManager 引用,异常就会消失并且事情似乎可以正常工作(关于 BillableTime;它当然会破坏 BillingManager 的持久性)。现在似乎存在一些“自引用”问题,因为 Person.BillingManager属性本身是对 Person 的引用.

    知道这里发生了什么吗?我不知所措...

    谢谢。

    === 类和映射 ===
    public class Person
    {
    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }

    public virtual Person BillingManager { get; set; }

    public virtual IList<ExpenseReport> ExpenseReports { get; set; }
    public virtual IList<BillableTime> Time { get; set; }
    }


    public class PersonMapping : ClassMap<Person>
    {
    public PersonMapping()
    {
    Id(p => p.UserId).GeneratedBy.Assigned();
    Map(p => p.LastName).Not.Nullable();
    Map(p => p.FirstName).Not.Nullable();

    References(p => p.BillingManager);

    HasMany(p => p.ExpenseReports).Cascade.AllDeleteOrphan();
    HasMany(p => p.Time).Cascade.AllDeleteOrphan();
    }
    }

    public class BillableTime
    {
    public virtual int Id { get; private set; }
    public virtual Week Week { get; set; }
    public virtual Person Person { get; set; }
    public virtual Project Project { get; set; }
    public virtual float[] DailyHours { get; set; }
    public virtual Invoice Invoice { get; set; }
    public virtual bool ReadOnly { get; set; }
    }

    public class BillableTimeMapping : ClassMap<BillableTime>
    {
    public BillableTimeMapping()
    {
    Id(x => x.Id);
    References(x => x.Week);
    References(x => x.Person);
    References(x => x.Project);
    References(x => x.Invoice);
    Map(x => x.ReadOnly).Not.Nullable().Default("0");
    Map(x => x.DailyHours).Length(28);
    }
    }


    public class ExpenseReport
    {
    public virtual long Id { get; set; }
    public virtual Person SubmittedBy { get; set; }
    }

    最佳答案

    下面这行应该可以解决这个问题,但我不知道为什么会这样。如果我有空闲时间,我会调查一下。

    HasMany(p => p.Time).Cascade.AllDeleteOrphan().KeyColumn("Person_Id");

    关于fluent-nhibernate - NHibernate 异常 : could not initialize a collection, 无效的列名。流畅的映射。也许是多对一的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7681928/

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