我有一个加载我的实体 ( City) 及其相关数据 ( PointsOfInterest)的存储库
public City GetCity(int cityId, bool includePointsOfInterest)
{
var city = _context.Cities.SingleOrDefault(x => x.Id == cityId);
if (includePointsOfInterest)
{
_context.Entry(city)
.Collection(x => x.PointsOfInterest)
.Load();
}
return city;
}
Run Code Online (Sandbox Code Playgroud)
为了测试这种方法,我决定使用 SQLLite InMemory,因为我可以测试 Eager 加载功能。
上下文的设置:
SqliteConnection connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<CityInfoContext>()
.UseSqlite(connection)
.Options;
var context = new CityInfoContext(options);
var cities = new List<City>()
{
new City()
{
Id = 1,
Name = "New York City",
Description = "The one …Run Code Online (Sandbox Code Playgroud)