小编Bru*_*lar的帖子

Entity Framework Core SQLite - 在测试中模拟 Eager Loading

我有一个加载我的实体 ( 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)

c# sqlite entity-framework-core

5
推荐指数
1
解决办法
343
查看次数

标签 统计

c# ×1

entity-framework-core ×1

sqlite ×1