使用字符串从DbContext检索数据

时间:2015-11-29 10:58:33

标签: c# entity-framework entity-framework-core

使用Entity Framework,您可以轻松地从数据库中获取数据,如下所示:

var allBooks = BookContext.Books;

因此,此代码会返回" Book"表

但是我收到的字符串告诉了我" table"我需要来自的数据。所以我有这样的事情:

public void GetData(string entity) // entity = "Book"
{
    // Get data using the "entity" string from BookContext
    BookContext.FromString(entity); // for exmaple
}

有没有办法使用字符串从BookContext检索数据?

1 个答案:

答案 0 :(得分:0)

快速查看source code of EF7表明,与之前的版本相比,Set(Type entityType)类中没有任何DbContext方法。只有它的通用版本Set<TEntity>()

所以我认为,如果不使用某些模糊的反射,就不可能。 (模糊,因为EF7没有DbSet类的任何非泛型版本,这使得使用反射获得的集合非常困难)

相关问题