实体框架核心-包含在本地或数据库搜索扩展方法中

时间:2018-08-02 08:08:16

标签: c# linq entity-framework-core

为了始终在本地数据库上下文而不是数据库本身中进行首次搜索并支持包含相关实体,我有以下扩展方法:

public static T LocalOrDatabase<T>(this DbSet<T> set, Expression<Func<T, bool>> criteria, params Expression<Func<T, object>>[] includes) where T : class {

    return LookUp(set.Local.AsQueryable(), criteria) ?? // Search local
           LookUp(set.AsQueryable(), criteria, includes); // Search database
}

private static T LookUp<T>(IQueryable<T> source, Expression<Func<T, bool>> criteria, params Expression<Func<T, object>>[] includes) where T : class {

    if (includes != null && includes.Any()) {
        foreach (Expression<Func<T, object>> include in includes) {

            source = source.Include(include);
        }
    }

    return source.FirstOrDefault(criteria);
}

假设这些示例实体:

[Table("entities1", Schema = "entities")]
public class Entity1
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Field1 { get; set; }

    public string Field2 { get; set; }

    public int? IdRelatedEntity1 { get; set; }

    [ForeignKey("IdRelatedEntity1")]
    public virtual RelatedEntity1 RelatedEntity1 { get; set; }

    public int? IdRelatedEntity2 { get; set; }

    [ForeignKey("IdRelatedEntity2")]
    public virtual RelatedEntity2 RelatedEntity2 { get; set; }

    public virtual List<RelatedEntity3> RelatedEntities3 { get; set; }
}

它的调用方式:

Expression<Func<Entity1, bool>> criteria = e1 =>
    e1.Field1 == X &&
    e1.Field2 == Y;

_context.Entity1.LocalOrDatabase(criteria,
    // Includes
    e1 => e1.RelatedEntity1,
    e1 => e1.RelatedEntity2);

尽管如此,通过使用此解决方案,我无法从Entity1包括实体RelatedEntity3的内部字段(RelatedEntity4),因为它们之间的关系不是一对一的。

预期行为:

_context.Entity1.LocalOrDatabase(criteria,
    // Includes
    e1 => e1.RelatedEntity1,
    e1 => e1.RelatedEntity2,
    e1 => e1.RelatedEntities3.RelatedEntity4);

我需要对“ ThenInclude”的行为进行整合。 有任何想法吗?我不在乎方法上的大改变。

0 个答案:

没有答案