Generic ObjectContext? objectContext.GetObjectSet<TEntity>?

只愿长相守 提交于 2019-12-25 00:43:23

问题


Is there a way to get ObjectQuery<T> for specfied generic type?

Pseudo:

public partial class MyObjectContext
{
    public ObjectSet<TEntity> GetObjectSet<TEntity>()
    {
        return Helper.GetObjectSet<TEntity>(this);
    }
}

回答1:


Yes this is what you need:

public partial class MyObjectContext
{
    public ObjectSet<TEntity> GetObjectSet<TEntity>()
    {
        return this.CreateObjectSet<TEntity>();
    }
}

As you can see your helper method is not needed because you can call CreateObjectSet directly on MyObjectContext instance. It will return ObjectSet<TEntity> which is derived from ObjectQuery<TEntity>. TEntity must be mapped type and it cannot be derived type in entity hierarchy.



来源:https://stackoverflow.com/questions/5925209/generic-objectcontext-objectcontext-getobjectsettentity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!