检查通用的方法如果实体存在在实体框架? [英] Generic Way to Check If Entity Exists In Entity Framework?

查看:121
本文介绍了检查通用的方法如果实体存在在实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似<一个href=\"http://stackoverflow.com/questions/1802286/best-way-to-check-if-object-exists-in-entity-framework\">Best检查的方式,如果在实体框架中存在的对象?

我在寻找一种通用的方式在 DbSet 来检查的实体。事情是这样的,它不工作:

 私有的DbContext的DbContext {搞定;组; }私人DbSet&LT; T&GT; DbSet {搞定;组; }公共布尔存在(T实体){
    在this.DbSet返回((从项目
             其中,item ==实体
             选择项目).Count之间的()&GT; 0);
}

,其中项目==实体在LINQ工程SQL,但显然无法与LINQ到实体。由于实体可能有不同的钥匙,我不能把它们都从比较已知关键的共同继承抽象

<击>我能做到这一点,但我很担心捕获异常作为验证过程的性能这不工作,要么因为只要实体分离的 OriginalValues​​ 属性:

 公共布尔存在(T实体){
    尝试{
        VAR电流= this.DbContext.Entry(实体).OriginalValues​​;
        如果实体是不在数据库还//不会达到这个线
        返回true;
    }
    赶上(例外前){
        返回false;
    }
}


解决方案

你想通用的方法来检查,如果实体是由环境或实体存在查询数据库通用的方法装?

有关在前一种情况下使用:

 公共BOOL存在&LT; T&GT;(T实体)
{
    返回this.Set&LT; T&GT;()Local.Any(E =&GT; e ==实体);
}

对于后一种情况使用(它会检查加载实体以及):

 公共BOOL存在&LT; T&GT;(params对象[]键)
{
    回报(this.Set&LT; T&GT;()查找(键)= NULL!);
}

编辑:

EF code首先是不应该访问这种信息,但它有可能获得实体键的名称。我觉得这样的事情应该工作:

  VAR objContext =((IObjectContextAdapter)的DbContext).ObjectContext;
VAR objSet = objContext.CreateObjectSet&LT; T&GT;();
VAR键名= objSet.EntitySet.ElementType.KeyMembers.Select(M = GT; m.Name);

但是,这一切没有意义。你想通用的做法,但你的实体不共享必要的信息,使通用的方法。现在你说,你甚至不知道键值。使用这种通用的做法,需要反思和前pression树的人工建筑。

Similar to Best way to check if object exists in Entity Framework?

I'm looking for a generic way to check for an entity in a DbSet. Something like this, which doesn't work:

private DbContext DbContext { get; set; }

private DbSet<T> DbSet { get; set; }

public Boolean Exists(T entity) {
    return ((from item in this.DbSet
             where item == entity
             select item).Count() > 0);
}

The line where item == entity works in LINQ to SQL, but apparently not with LINQ to Entities. Since the entities may have different keys I can't have them all inherit from a common abstract with a known key for comparison.

I could do this, but I'm worried about the performance of catching exceptions as a verification process This doesn't work either since as long as the entity is detached the OriginalValues property can't be obtained:

public Boolean Exists(T entity) {
    try {
        var current = this.DbContext.Entry(entity).OriginalValues;
        // Won't reach this line if the entity isn't in the database yet
        return true;
    }
    catch (Exception ex) {
        return false;
    }
}

解决方案

Do you want generic way to check if entity was loaded by context or generic way to query database if entity exists?

For the former case use:

public bool Exists<T>(T entity)
{
    return this.Set<T>().Local.Any(e => e == entity);
}

For the latter case use (it will check loaded entities as well):

public bool Exists<T>(params object[] keys)
{
    return (this.Set<T>().Find(keys) != null);
}

Edit:

EF code first is not supposed to access this kind of information but it is possible to get name of entity keys. I think something like that should work:

var objContext = ((IObjectContextAdapter)dbContext).ObjectContext;
var objSet = objContext.CreateObjectSet<T>();
var keyNames = objSet.EntitySet.ElementType.KeyMembers.Select(m => m.Name);

But this all doesn't make sense. You want generic approach but your entities doesn't share necessary information to allow generic approach. Now you say that you even don't know key values. Using this "generic" approach will require reflection and manual building of expression tree.

这篇关于检查通用的方法如果实体存在在实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆