我有以下類:功能NHibernate - 不能添加的項目與持久性數據的hasMany關係
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.
我錯過或誤解這裏的東西?而我需要做什麼修改,以便能夠將項目添加到這個列表堅持?
您可以包括堆棧跟蹤?我能夠創建這個樣本,它爲我工作得很好嗎? – codeprogression
堆棧跟蹤是很無趣......它指向客戶端類(我加了函數引發錯誤)的指示線,低於這只是我的程序 –