EntityFramework Get object by ID?

前端 未结 8 647
旧巷少年郎
旧巷少年郎 2020-12-23 20:48

Is it possible with Generics to get an object from my EntityFramework without knowing the type?

I\'m thinking of something along the lines of:

public         


        
8条回答
  •  有刺的猬
    2020-12-23 21:35

    I think this could help

    public static TEntity Find(this ObjectSet set, object id) where TEntity : EntityObject
        {
          using (var context = new MyObjectContext())
          {
            var entity = set.Context.CreateObjectSet();
            string keyName = entity.FirstOrDefault().EntityKey.EntityKeyValues.FirstOrDefault().Key;
    
            return entity.Where("it." + keyName + " = " + id).FirstOrDefault();
          }
        }
    

提交回复
热议问题