难道实体框架有DataContext.GetTable&LT的等效; TEntity>从LINQ2SQL(ObjectContext.CreateQuery< T>?) [英] Does the Entity Framework have an equivalent of DataContext.GetTable<TEntity> from Linq2Sql (ObjectContext.CreateQuery<T>?)

查看:134
本文介绍了难道实体框架有DataContext.GetTable&LT的等效; TEntity>从LINQ2SQL(ObjectContext.CreateQuery< T>?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在寻找的 DataContext.GetTable&LT的等效; TEntity>在实体框架。 我已经找到了 ObjectContext.CreateQuery< T> 方法,但它是从 DataContext.GetTable&LT不同,TEntity> 自它需要一个查询字符串工作。


I'm looking for an equivalent of the DataContext.GetTable<TEntity> in Entity Framework. I've found the ObjectContext.CreateQuery<T> method but it is different from DataContext.GetTable<TEntity> since it needs a querystring to work.

有没有一种方法可以使用​​该实体类型的表一个IQueryable对象,而无需指定查询字符串?

Is there a way to get an IQueryable object for a table using the entity type without specifying the querystring?

*编辑:增加了code段*
这是一个与LINQ2SQL工作存储库类,我实现的一个片段。我不能使用 ObjectContext的。[表名] ,因为它不会是通用的了。

* Added code snippet*
This is a snippet of a Repository class I've implemented that works with linq2sql. I can't use ObjectContext.[TableName] because it wouldn't be generic anymore.

public class BaseRepository<TClass> : IDisposable
        where TClass : class
    {
        protected BaseRepository(DataContext database)
        {
            _database = database;
        }
        ...

        public IQueryable<TClass> GetAllEntities()
        {
            IQueryable<TClass> entities = _database.GetTable<TClass>();
            return entities;
        }

        public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
        {  
            IQueryable<TClass> table = _database.GetTable<TClass>();
            return table.Where(condition);    
        }

*编辑:增加了我的解决方案(到目前为止..)*
这是我使用的是什么:

* Added my solution (so far..)*
This is what I'm using:

public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
{  
    IQueryable<TClass> table = _database.CreateQuery<TClass>(typeof(TClass).Name);
    return table.Where(condition);    
}

这个工作,只要类名是相同的表名。这将成为一个问题,对我来说,当我开始使用不同的对象同一个表。

This works as long as the class name is the same of the table name. This will became a problem for me when I'll start using different objects for the same table.

我希望我已经清楚,在此先感谢,
马可:)

I hope I've been clear, thanks in advance,
Marco :)

推荐答案

其实,EF设计者本身使用的createQuery 与硬codeD字符串的静态引用。如果你深入到设计文件,你会看到这样的内容:

Actually, the EF designer itself uses CreateQuery with hard-coded strings for the static references. If you dig into the designer file you'll see something like this:

public global::System.Data.Objects.ObjectQuery<Customers> Customers
{
    get
    {
        if ((this._Customers == null))
        {
            this._Customers = base.CreateQuery<Customers>("[Customers]");
        }
        return this._Customers;
    }
}

private global::System.Data.Objects.ObjectQuery<Customers> _Customers;

在技术上有没有完美的解决方案,因为你可以使用不同的实体集相同的实体类型。但是你可以给它的老学院尝试:

Technically there's no perfect solution because you can use the same entity type for different entity sets. But you can give it the old college try:

public IQueryable<TEntity> GetEntities<TEntity>()
{
    Type t = typeof(TEntity);
    var edmAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(t,
        typeof(EdmEntityTypeAttribute), false);
    if (edmAttr == null)  // Fall back to the naive way
    {
        return context.CreateQuery<TEntity>(t.Name);
    }
    var ec = context.MetadataWorkspace.GetEntityContainer(
        context.DefaultContainerName, DataSpace.CSpace);
    var entityType = context.MetadataWorkspace.GetType(edmAttr.Name,
        edmAttr.NamespaceName, DataSpace.CSpace);
    var es = ec.BaseEntitySets.First(es => es.ElementType == entityType);
    return context.CreateQuery<TEntity>(es.Name);
}

这篇关于难道实体框架有DataContext.GetTable&LT的等效; TEntity&GT;从LINQ2SQL(ObjectContext.CreateQuery&LT; T&GT;?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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