实体框架核心和Cosmos DB。使用LINQ表达式读取实体

时间:2020-02-14 08:28:42

标签: c# azure entity-framework-core azure-cosmosdb entity-framework-core-3.1

在我们的解决方案中,我们使用软件包Microsoft.EntityFrameworkCore.Cosmos 3.1.1对Azure中的cosmos数据库和容器进行操作。我们有一个相当简单的对象结构。一个对象包含其他对象的列表。

public class ExampleEntity : Entity
{
    public string TestProperty { get; set; }
    public IEnumerable<SubEntity> SubEntities { get; set; }

    protected override IEnumerable<object> GetEqualityComponents()
    {
            yield return TestProperty;
    }
}

public class SubEntity : Entity
{
    public bool IsActive { get; set; }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return IsActive;
    }
}

我们为EntityFramework配置了DbContext,如下所示:

builder.Entity<ExampleEntity>().ToContainer(nameof(ExampleEntity));
builder.Entity<ExampleEntity>().HasKey(p => p.id);
builder.Entity<ExampleEntity>().OwnsMany(p => p.SubEntities);

cosmos中的json结构如下:

{
    "id": "51099fa9-5d71-4181-93b1-2c8cc0482a95",
    "CreatedAt": "2020-02-14T08:11:06.701659Z",
    "Discriminator": "ExampleEntity",
    "TestProperty": "Property1",
    "UpdatedAt": "0001-01-01T00:00:00",
    "SubEntities": [
        {
            "id": "9a120613-c42a-4399-a660-e6228cfce0ad",
            "CreatedAt": "2020-02-14T08:11:06.70457Z",
            "ExampleEntityid": "51099fa9-5d71-4181-93b1-2c8cc0482a95",
            "IsActive": false,
            "UpdatedAt": "0001-01-01T00:00:00"
        },
        {
            "id": "21b86b53-2d6a-4b31-a60b-8d31cfd04734",
            "CreatedAt": "2020-02-14T08:11:06.705145Z",
            "ExampleEntityid": "51099fa9-5d71-4181-93b1-2c8cc0482a95",
            "IsActive": true,
            "UpdatedAt": "0001-01-01T00:00:00"
        }
    ],
    "_rid": "R343APAECLsBAAAAAAAAAA==",
    "_self": "dbs/R343AA==/colls/R343APAECLs=/docs/R343APAECLsBAAAAAAAAAA==/",
    "_etag": "\"06001f30-0000-0d00-0000-5e46561b0000\"",
    "_attachments": "attachments/",
    "_ts": 1581667867
}

现在,我们要在ExampleEntities的布尔值SubEntities设置为true的IsActive之后进行搜索。这是我们的问题开始的地方。

我们有一个通用的存储库,其中的Read方法如下所示:

/// <summary>
/// Get an entity from the database
/// </summary>
/// <param name="predicate">A predicate to decide which entity to get</param>
/// <param name="children">Child entities to included in the DbSet</param>
/// <returns>All entities that matches the predicate</returns>
public async Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] children)
{
    var dbSet = _dbContext.Set<TEntity>();
    children.ToList().ForEach(p => dbSet.Include(p));

    var entities = dbSet.Where(predicate);
    return await entities.ToListAsync();
}

在IntegrationTest中使用以下代码:

[Test]
public async Task EntityFramework_Should_Return_Object_Based_On_Property_In_SubEntity()
{
    var uow = Container.Resolve<IUnitOfWork<ExampleEntity>>();
    var entity1 = new ExampleEntity
    {
        TestProperty = "Property1",
        SubEntities = new List<SubEntity>
        {
            new SubEntity
            {
                IsActive = false
            },
            new SubEntity
            {
                IsActive = true
            }
        }
    };

    await uow.Repository.CreateAsync(entity1);
    await uow.CommitAsync();

    var readEntity = uow.Repository.ReadAsync(p => p.SubEntities.Any(p => p.IsActive), p => p.SubEntities);
    readEntity.Should().NotBeNull();
}

此行出现问题,在这里我使用上面存储库中的Read方法:

var readEntity = uow.Repository.ReadAsync(p => p.SubEntities.Any(p => p.IsActive), p => p.SubEntities);

它导致以下异常:

System.InvalidOperationException: The LINQ expression 'DbSet<ExampleEntity>
    .Where(e => EF.Property<IEnumerable<SubEntity>>(e, "SubEntities")
        .AsQueryable()
        .Any(o => o.IsActive))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

我很奇怪,Entity Framework Cosmos不支持这样的简单查询。显然,我可以做一个AsEnumerable(),但是我将从数据库下载所有数据,并在客户端而不是数据库侧进行过滤,当数据库包含100´000的数据库时,这将对性能产生巨大的影响。记录..

如何在数据库方面重写存储库以进行此类过滤? Entity Framework Cosmos完全可行吗?

1 个答案:

答案 0 :(得分:2)

根据current limitations Includejoin尚不支持。