0

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?

  • Can you include the stacktrace? I was able to create this as a sample and it worked fine for me? – codeprogression Jul 21 '11 at 21:59
  • The stacktrace is pretty uninteresting... it points to the indicated line in the Client class (I added the function throwing the error), below that is just my program – Matt Haines Jul 21 '11 at 22:15

2 Answers2

1

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

codeprogression
  • 3,371
  • 1
  • 22
  • 16
  • You are correct. After some digging, I figured out this was being caused by some voodoo framework code that automagically serializes objects inherited off a couple classes - when the list got serialized, it converted it to a static sized array. – Matt Haines Jul 21 '11 at 22:37
0

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();
Smokefoot
  • 1,695
  • 2
  • 10
  • 13
  • I want Client to control the ClientMonthlyRevenue object so that I never need to explicitly save the child object. – Matt Haines Jul 21 '11 at 22:04
  • Adding Inverse() just changes which mapping controls the save. By calling Cascade.All() your clientmap is still instigating the save, but deferring the save operation to the revenuemap. This makes a difference in the sql generated. Without inverse, you will see a bunch of DELETE operations, then INSERT operations. With inverse, you will see the one INSERT operation. – codeprogression Jul 21 '11 at 22:08
  • Wow, I totally misunderstood what Inverse() was for.. Thanks! Sadly, it did not help the problem. – Matt Haines Jul 21 '11 at 22:18