我正在學習實體框架,並面對一些時刻與查找()方法我無法理解。從朱莉婭·勒曼的書採取
樣本「編程實體框架:代碼優先」實體框架的Find方法如何工作?
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte?[] Photo { get; set; }
public ICollection<Lodging> Lodgings { get; set; }
public Destination()
{
Lodgings = new List<Lodging>();
}
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public Destination Destination { get; set; }
}
我的數據在以下幾個方面工作:
var destination = organizationDbContext.Destinations // case # 1
.Include("Lodgings")
.First(p=>p.DestinationId==1);
var destination = organizationDbContext.Destinations.Find(1); // case # 2
- 爲什麼我可以」在Include()調用後的第一種情況下調用Find()方法,但可以使用Where()和First()?
- 如果我在Find()方法中使用第二種情況,在這裏我不能調用Include()方法到寄宿,所以我應該如何加載相關的寄宿?
- 什麼是應該做的正確的方法:找一個對象,並加載所有相關的內部對象(一到多)
我的問題可以用另一種方式來表達?
- 什麼是正確的做法:加載所有對象(集合A)和內部相關對象(集合A.I),然後通過(A)中的id找到一個?
據我所知'IEnumerable的'不包含方法'Find'但' DbSet '確實。這就是爲什麼它在#2的情況下 –
Fabjan