【问题标题】:Look up locally cached entity by key in EF 6 / Code First在 EF 6 / Code First 中按键查找本地缓存的实体
【发布时间】:2015-05-15 18:25:28
【问题描述】:

EF 6,代码优先

我希望为 EF 6 DbContext 编写一个 TryGetLocalEntity() 方法,该方法将按键查找一个实体,如果它存在于 DbContext.Set<TEntity().Local 中,则返回它。我想根据实体键而不是对象引用进行查找。

所以方法签名可能是:

    public static bool TryGetLocalEntity<TContext, TEntity>(this TContext context, TEntity entity, out TEntity cachedEntity)
        where TContext : DbContext
        where TEntity : class
    {

        EntityKey key = ???GetEntityKey???(entity);
        cachedEntity = context.Set<TEntity>().Local.Where(e => ???).FirstOrDefault();
        return cachedEntity != null;
    }

这样做的目的是获取一个已从另一个源反序列化的实体,或者 1) Attach() 如果它在本地不存在(由实体键而不是对象引用确定),则它到 DbContext 或2) 检索本地缓存的实体(如果它确实存在)(再次由实体键而不是对象引用确定)。我知道我可以从数据库中获取它,但如果我已经拥有我需要的东西,我想避免往返。

我认为我缺少的主要步骤是从分离的 POCO 实体创建 EntityKey。从这里,我想我可以根据EntityKey 弄清楚如何在DbContext 中查找。

更新这是一个破解。显然有一些技巧,我还没有测试过:

using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.Pluralization;

    public static bool TryGetLocalEntity<TContext, TEntity>(this TContext context, TEntity entity, out TEntity cachedEntity)
        where TContext : DbContext
        where TEntity : class
    {
        EnglishPluralizationService pluralizationService = new EnglishPluralizationService();
        string setName = pluralizationService.Pluralize(typeof(TEntity).Name);
        ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
        EntityKey key = objectContext.CreateEntityKey(setName, entity);
        try
        {
            cachedEntity = objectContext.GetObjectByKey(key) as TEntity;
        }
        catch (ObjectNotFoundException)
        {
            cachedEntity = null;
        }
        return cachedEntity != null;
    }

更新当实体存在于上下文中但尚未保存到数据库中时,我所拥有的似乎工作除了。我一直在 EF 源中进行挖掘,试图确定未保存/添加的实体是否没有 EntityKey

【问题讨论】:

    标签: c# entity-framework ef-code-first entity-framework-6


    【解决方案1】:

    你可以使用:

    var objContext = ((IObjectContextAdapter)context).ObjectContext;
    var objSet = objContext.CreateObjectSet<TEntity>();
    var entityKey = objContext.CreateEntityKey(objSet.EntitySet.Name, entity);
    

    【讨论】:

    • 嘿嘿,好像是 4 分钟前,我可能正在打字:/
    • 我在完善它的同时不断编辑,以免人们浪费时间。也许不是最好的方法哈哈
    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2015-04-01
    相关资源
    最近更新 更多