我有2個相關的Linq to SQL問題。請參閱下面的圖片以查看我的模型的外觀。在自引用表中急切地加載Linq到SQL實體
問題1
我試圖找出如何渴望負載我User
類/表中的User.AddedByUser
領域。該字段是根據User.AddedByUserId
字段中的關係生成的。該表是自引用的,我試圖找出如何讓Linq to SQL加載User.AddedByUser
屬性,即任何User
實體被加載/獲取時,它還必須獲取User.AddedByUser和User.ChangedByUser 。不過,據我所知,這可能成爲一個遞歸問題...
更新1.1:
我試圖使用DataLoadOptions如下:
var options = new DataLoadOptions();
options.LoadWith<User>(u => u.ChangedByUser);
options.LoadWith<User>(u => u.AddedByUser);
db = new ModelDataContext(connectionString);
db.LoadOptions = options;
但這並不工作,我得到以下例外的第2行:
System.InvalidOperationException occurred
Message="Cycles not allowed in LoadOptions LoadWith type graph."
Source="System.Data.Linq"
StackTrace:
at System.Data.Linq.DataLoadOptions.ValidateTypeGraphAcyclic()
at System.Data.Linq.DataLoadOptions.Preload(MemberInfo association)
at System.Data.Linq.DataLoadOptions.LoadWith[T](Expression`1 expression)
at i3t.KpCosting.Service.Library.Repositories.UserRepository..ctor(String connectionString) in C:\Development\KP Costing\Trunk\Code\i3t.KpCosting.Service.Library\Repositories\UserRepository.cs:line 15
InnerException:
該例外是相當不言自明 - 對象圖不允許爲循環。 另外,假設第2行沒有拋出異常,我很確定第3行會,因爲它們是重複的鍵。
更新1.2:
下列不工作,要麼(在一起選擇不使用更新1.1以上):
var query = from u in db.Users
select new User()
{
Id = u.Id,
// other fields removed for brevityy
AddedByUser = u.AddedByUser,
ChangedByUser = u.ChangedByUser,
};
return query.ToList();
它引發以下,不言自明的例外:
System.NotSupportedException occurred
Message="Explicit construction of entity type 'i3t.KpCosting.Shared.Model.User' in query is not allowed."
我現在真的很在意如何解決這個問題。請幫忙!
問題2
在我的DB所有其他表,因此LINQ到SQL模式,我有兩個字段,Entity.ChangedByUser
(與Entity.ChangedByUserId
外鍵/關係)和Entity.AddedByUser
(與Entity.AddedByUserId
外鍵/ relationship)
如何讓Linq to SQL真正爲我加載這些字段?我是否需要對我的查詢做一個簡單的連接?或者是否有其他方法?
是的我想在我的GUI中顯示該信息。我希望能夠在不使用匿名類型的情況下解決這個問題。但我想這是解決問題的一種方式。我的問題與Linq to SQL無法應對多對多關係有關嗎? – 2009-09-08 14:27:32
Na,您可以將User.AddedDescription和User.ChangedDescription字段添加到User類,然後您不必使用anon類型。 這與許多關係(你的例子中的每個關係都是1:1)沒有任何關係。 如果你直接編寫你的SQL,你不想獲得AddedByUser的所有屬性(包括他的AddedByUser等等無窮大)。你可能會抓住他的名字和/或你需要的任何細節。 伊莫這是一個更普遍的O/RM問題。我想你會遇到與L2E或Hibernate相同的問題。 – 2009-09-09 10:56:41