Reputation: 48
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?
Upvotes: 0
Views: 1219
Reputation: 3441
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
Upvotes: 1
Reputation: 1805
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();
Upvotes: 0