0
我是一個完整的流利新手,雖然我發現了很多關於多對多連接的網絡,我沒有發現在我目前的情況很多。問題自我許多人加入流利NHibernate
注:好吧,這可能是垃圾設計,我可能會更好做單獨的表時,有關設計侮辱被讚賞:)
我有一個表像這樣:
和類如:
[Serializable]
public class Transaction : Entity
{
[DomainSignatureAttribute]
public virtual long TransactionId { get; protected internal set; }
public virtual long AccountId { get; set; }
protected internal virtual short TransactionTypeId { get; set; }
protected internal virtual short TransactionStatusId { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual DateTime DateEffective { get; set; }
public virtual Decimal Amount { get; set; }
public virtual IList<TransactionLink> ChildTransactions { get; set; }
public virtual IList<TransactionLink> ParentTransactions { get; set; }
public Transaction()
{
ChildTransactions = new List<TransactionLink>();
ParentTransactions = new List<TransactionLink>();
}
/// <summary>
/// Use this to add a payment to a charge to pay it off, or to add a refund on a payment to un-pay it
/// </summary>
/// <param name="inTransaction"></param>
/// <param name="inAmount"></param>
public virtual void AddChildTransaction(Transaction inTransaction, Decimal inAmount)
{
TransactionLink link = new TransactionLink()
{
TransactionParent = this,
TransactionChild = inTransaction,
Amount = inAmount
};
if (!ChildTransactions.Contains(link))
ChildTransactions.Add(link);
if (!inTransaction.ParentTransactions.Contains(link))
inTransaction.ParentTransactions.Add(link);
}
/// <summary>
/// You probably shouldnt be using this.
/// </summary>
/// <param name="inTransaction"></param>
/// <param name="inAmount"></param>
public virtual void AddParentTransaction(Transaction inTransaction, Decimal inAmount)
{
TransactionLink link = new TransactionLink()
{
TransactionChild = this,
TransactionParent = inTransaction,
Amount = inAmount
};
if (!inTransaction.ChildTransactions.Contains(link))
inTransaction.ChildTransactions.Add(link);
if (!ParentTransactions.Contains(link))
ParentTransactions.Add(link);
}
}
而且
[Serializable]
public class TransactionLink : Entity
{
public virtual Int64 TransactionIdParent { get; protected internal set; }
public virtual Transaction TransactionParent { get; set; }
public virtual Int64 TransactionIdChild { get; set; }
public virtual Transaction TransactionChild { get; protected internal set; }
public virtual DateTime LastModifiedOn { get; set; }
public virtual Decimal Amount { get; set; }
#region Override comparison - as this is a composite key we need to custom roll our comparison operators
#endregion
}
個
地圖:
public TransactionMap()
{
Id(x => x.TransactionId);
Map(x => x.AccountId);
Map(x => x.TransactionTypeId);
Map(x => x.TransactionStatusId);
Map(x => x.DateCreated);
Map(x => x.DateEffective);
Map(x => x.Amount);
HasMany(x => x.ParentTransactions).Cascade.None().KeyColumn("TransactionIdParent").LazyLoad();
HasMany(x => x.ChildTransactions).Cascade.All().KeyColumn("TransactionIdChild").LazyLoad();
}
和
public TransactionLinkMap()
{
CompositeId()
.KeyProperty(x => x.TransactionIdParent, "TransactionIdParent")
.KeyProperty(x => x.TransactionIdChild, "TransactionIdChild");
References(x => x.TransactionParent).Column("TransactionIdParent").Cascade.SaveUpdate().LazyLoad();
References(x => x.TransactionChild).Column("TransactionIdChild").Cascade.All().LazyLoad();
Version(x => x.LastModifiedOn);
Map(x => x.Amount);
}
我身邊調整的事情,讓各種錯誤從NHibernate的 - 有人請告訴我什麼是去了解它的最好方法?
看起來像這個問題http://stackoverflow.com/questions/2243251/fluent-nhibernate-mapping-problem-many-to-many-self-join-with-additional-data – 2011-03-11 13:00:51
謝謝喬納斯,這個Q是在那裏我開始了映射看起來相似,但是我的創造問題。我得到以下內容:具有相同標識符值的不同對象已與會話相關聯:TransactionSystem.TransactionLink,此時實體:TransactionSystem.TransactionLink正確。 – 2011-03-11 13:53:22