我使用的是代碼優先的實體框架,其基本上下文僅由標準IDbSet集合組成,其中T只是一個POCO類。在我的上下文中,我禁用了懶加載。雖然我的模型類中有「導航屬性」,但我已經從它們中刪除了虛擬關鍵字。實體框架加載相關實體
存儲庫中的「全部獲取」方法會執行一些自定義篩選,以確保當前用戶只能看到他們擁有的數據,除非他們是管理員。我遇到了一個特殊的問題,我以管理員身份登錄,該管理員也與某些記錄相關聯。由於我登錄的實體是在上下文中加載的,即使我已禁用延遲加載,虛擬刪除並且未使用包含或加載,結果中與我的配置文件具有關聯的對象具有導航屬性自動設置。
這不是我項目的代碼,只是一個例子來展示我在做什麼的想法。它可能有錯別字和語法錯誤。
public class Record
{
public Guid Id { get; set; }
public string Name { get; set; }
public Owner Owner { get; set; } //No virtual keyword
public Guid OwnerId { get; set; }
}
public class Owner
{
public Guid Id { get; set; }
public string Name { get; set; }
public Collection<Record> Records { get; set; } //No virtual keyword
}
public class Context : DbContext
{
IDbSet<Owner> Owners { get; set; }
IDbSet<Record> Records { get; set; }
public static Context Create()
{
Context context = new Context();
context.Configuration.LazyLoadingEnabled = false; //Lazy loading disabled
return context;
}
}
public class Repository
{
private Context Context { get; set; }
public Owner CurrentOwner { get; private set; }
public Repository()
{
Context = Context.Create();
//Code here to get the application user and look up an associated "owner"
//entity if the user is an "owner" (they could just be an administrator)
//but the GetCurrentOwnerOrNull uses the Context to find the user
CurrentOwner = GetCurrentOwnerOrNull();
}
public IQueryable<Record> GetRecords(bool asAdmin)
{
IQueryable<Record> records = Context.Records; //Not including or loading Owner
if (asAdmin)
{
//Verify that the application user is an admin and throw exception otherwise
}
else
{
if (CurrentOwner == null)
{
//Throw a security exception
}
records = records.Where(r => r.OwnerId == CurrentOwner.Id);
}
return records;
}
}
如此反覆,與上面的問題是,如果我是運行該代碼作爲業主,是否不論是不是管理員,那麼這些是我自己的記錄將Owner屬性設置,而不是空。我希望實體框架擺脫我的業務,而不是自動設置它。它會導致下游出現問題,尤其是以管理員和所有者身份運行代碼時,所以您可以使用Owner = null和一些擁有Owner設置的記錄返回一些記錄。它很煩人。請讓我停下來。
作爲更新,我只是測試,如果在代碼中GetCurrentOwnerOrNull我創建一個新的上下文來做這個查詢,然後處理它,它擺脫了這個問題......但我離開問題打開現在如果有人知道另一種方法來簡單地在Entity Framework中關閉「功能」。 – 2014-08-29 18:50:31
你是否需要你的上下文*將*保存到緩存中的這些對象上? – 2014-08-29 18:52:16