我有以下幾點:如何擴展EF儲存庫模型以執行更復雜的查詢?
public partial class Subject
{
public Subject()
{
this.Contents = new List<Content>();
}
public int SubjectId { get; set; }
public string Name { get; set; }
public virtual ICollection<Content> Contents { get; set; }
}
public partial class Content
{
public int ContentId { get; set; }
public int ContentTypeId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public int SubjectId { get; set; }
public virtual Subject Subject { get; set; }
}
在我的SQL Server數據庫中,我有SubectId和ContentTypeId
的內容表中的索引我班正在尋找與具有這樣的方法,例如標準庫然而,使用存儲庫模型GetAll()和GetId(id)有一種方法可以執行更復雜的查詢。在這種情況下,我會以某種方式想要查詢特定的SujectId和contentTypeId。我想要避免的是查詢獲取每個內容記錄,然後篩選出我需要的內容。我想以某種方式發送一個真正的查詢,確切地說我需要SQL Server。
目前我通用倉庫有以下幾點:
public virtual T GetById(int id)
{
return DbSet.Find(id);
}
我能做什麼,我需要通過實施創建ContentRepository並具有類似如下:如果是的話我怎麼會
public IQuerable<Content> GetAllBySubjectId(int id)
{
return DbSet.Where(c => c.SubjectId == id);
}
使用GetAllBySubjectId並在檢查ContentId ==「01」的地方添加例如?
[Linq to Entities](http://msdn.microsoft.com/en-us/library/bb386964.aspx)的使用將導致發送實際查詢數據庫。 – tpeczek