Fluent NHibernate - 无法将项目添加到具有持久数据的 HasMany 关系中

发布于 2024-11-25 16:10:44 字数 2429 浏览 1 评论 0原文

我有以下类:

public class Client {
    public virtual Guid ClientID { get; set; }
    public virtual string ClientName { get; set; }
    public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }

    ...
    public virtual void SetMonthlyRevenue(int year, int month, double revenue)
    {   
        // Make sure it's not null... this might happen depending on how the client is created    
        if (Revenue == null)
        Revenue = new List<ClientMonthlyRevenue>();

        // Check for existance - we don't want any duplicates        
        ClientMonthlyRevenue clientMonthlyRevenue = Revenue.Where(x => x.Year == year && x.Month == month).FirstOrDefault();        
        if (clientMonthlyRevenue == null)        
        {
            // If it doesn't exist, create a new one and add to the list
            clientMonthlyRevenue = new ClientMonthlyRevenue(this, year, month, revenue);
            this.Revenue.Add(clientMonthlyRevenue);   // This is the line throwing the error
        }
        else
        {
            // If it exists, just update it
            clientMonthlyRevenue.Revenue = revenue;
        }
    }
}

public class ClientMonthlyRevenue {
    public virtual Client ParentClient { get; set; }
    public virtual int Year { get; set; }
    public virtual int Month { get; set; }
    public virtual double Revenue { get; set; }

    ...
}

以及这两个映射:

public class ClientMap : ClassMap<Client>
{
    Id(x => x.ClientID).GeneratedBy.Assigned();
    Map(x => x.ClientName);

    HasMany<ClientMonthlyRevenue>(x => x.Revenue)
        .Table("ClientMonthlyRevenue")
        .KeyColumn("ClientID")
        .Cascade.All()
        .Fetch.Join();
}

public class ClientMonthlyRevenueMap : ClassMap<ClientMonthlyRevenue>
{
    CompositeId()
        .KeyReference(x => x.Client, "ClientID")
        .KeyProperty(x => x.Year)
        .KeyProperty(x => x.Month);

    Map(x => x.Revenue);
}

当我从数据库获取客户端时:

Client client = Session.Get<Client>(clientID);

所有数据都在那里,这很棒。但是,当我尝试添加新的 ClientMonthlyRevenue 子项时:

client.Revenue.Add(new ClientMonthlyRevenue(this.ClientID, year, month, revenue));

我收到错误:

Collection was of a fixed size. 

我是否在这里遗漏或误解了某些内容?我需要修改什么才能将项目添加到这个持久列表中?

I've got the following classes:

public class Client {
    public virtual Guid ClientID { get; set; }
    public virtual string ClientName { get; set; }
    public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }

    ...
    public virtual void SetMonthlyRevenue(int year, int month, double revenue)
    {   
        // Make sure it's not null... this might happen depending on how the client is created    
        if (Revenue == null)
        Revenue = new List<ClientMonthlyRevenue>();

        // Check for existance - we don't want any duplicates        
        ClientMonthlyRevenue clientMonthlyRevenue = Revenue.Where(x => x.Year == year && x.Month == month).FirstOrDefault();        
        if (clientMonthlyRevenue == null)        
        {
            // If it doesn't exist, create a new one and add to the list
            clientMonthlyRevenue = new ClientMonthlyRevenue(this, year, month, revenue);
            this.Revenue.Add(clientMonthlyRevenue);   // This is the line throwing the error
        }
        else
        {
            // If it exists, just update it
            clientMonthlyRevenue.Revenue = revenue;
        }
    }
}

public class ClientMonthlyRevenue {
    public virtual Client ParentClient { get; set; }
    public virtual int Year { get; set; }
    public virtual int Month { get; set; }
    public virtual double Revenue { get; set; }

    ...
}

And these two mappings:

public class ClientMap : ClassMap<Client>
{
    Id(x => x.ClientID).GeneratedBy.Assigned();
    Map(x => x.ClientName);

    HasMany<ClientMonthlyRevenue>(x => x.Revenue)
        .Table("ClientMonthlyRevenue")
        .KeyColumn("ClientID")
        .Cascade.All()
        .Fetch.Join();
}

public class ClientMonthlyRevenueMap : ClassMap<ClientMonthlyRevenue>
{
    CompositeId()
        .KeyReference(x => x.Client, "ClientID")
        .KeyProperty(x => x.Year)
        .KeyProperty(x => x.Month);

    Map(x => x.Revenue);
}

When I get a Client from the database:

Client client = Session.Get<Client>(clientID);

all the data is there, which is great. But when I try to add a new ClientMonthlyRevenue child:

client.Revenue.Add(new ClientMonthlyRevenue(this.ClientID, year, month, revenue));

I get the error:

Collection was of a fixed size. 

Am I missing or misunderstanding something here? And what do I need to modify to be able to add items to this persisted list?

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

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

发布评论

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

评论(2

琉璃繁缕 2024-12-02 16:10:44

我会将 Client 对象更改为具有以下内容:

public class Client
{
    public Client()
    {
        Revenue = new List<ClientMonthlyRevenue>();
    }

    public virtual Guid ClientID { get; set; }
    public virtual string ClientName { get; set; }
    public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }

    public virtual void AddRevenue(ClientMonthlyRevenue revenue)
    {
        revenue.ParentClient = this;
        Revenue.Add(revenue);
    }
}

然后您可以像这样调用:

public void TestMapping()
{
    session.BeginTransaction();
    var client = new Client{ClientID = Guid.NewGuid()};
    session.SaveOrUpdate(client);

    client = session.Get<Client>(client.ClientID);
    client.AddRevenue(new ClientMonthlyRevenue(2001,07,1200));
    session.Transaction.Commit();
}

您收到的错误听起来像是可以在堆栈的更高位置创建的。我能够重现你的场景。查看完整来源:https://gist.github.com/1098337

I would change the Client object to have the following:

public class Client
{
    public Client()
    {
        Revenue = new List<ClientMonthlyRevenue>();
    }

    public virtual Guid ClientID { get; set; }
    public virtual string ClientName { get; set; }
    public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }

    public virtual void AddRevenue(ClientMonthlyRevenue revenue)
    {
        revenue.ParentClient = this;
        Revenue.Add(revenue);
    }
}

Then you can call like this:

public void TestMapping()
{
    session.BeginTransaction();
    var client = new Client{ClientID = Guid.NewGuid()};
    session.SaveOrUpdate(client);

    client = session.Get<Client>(client.ClientID);
    client.AddRevenue(new ClientMonthlyRevenue(2001,07,1200));
    session.Transaction.Commit();
}

The error you are receiving sounds like it could be created higher up in the stack. I was able to recreate your scenario. See full source: https://gist.github.com/1098337

忱杏 2024-12-02 16:10:44

您是否尝试过将您的收藏标记为反向?我不知道是否有帮助。

HasMany<ClientMonthlyRevenue>(x => x.Revenue)
    .Table("ClientMonthlyRevenue")
    .KeyColumn("ClientID")
    .Cascade.All()
    .Fetch.Join()
    .Inverse();

have you tried to mark your collection as Inverse? I dont know if it could help.

HasMany<ClientMonthlyRevenue>(x => x.Revenue)
    .Table("ClientMonthlyRevenue")
    .KeyColumn("ClientID")
    .Cascade.All()
    .Fetch.Join()
    .Inverse();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文