将表名传递给dbContext并从Entity Framework中的表中获取值(Pass table name to dbContext and get values from table in Entity Framework)
我有一个DbContext类,我在我的应用程序中首先使用代码。 我有几个包含"Id"和"Value"列的常见标准表,我想查询它们传递表名和列名,但实体框架中没有选项可以传递。
例:
常用表格:
Client_ClientDepartment (Id, Value) Client_ClientDesignation (Id, Value) Client_ClientCompany (Id, Value)我想要做的是传递表名和Id来获取值。 我创建了一个常用的方法
public string GetClientValue(string id, string tableName) { DatabaseContext dbContext = new DatabaseContext(); //Query the database and get value based on table and id. string value = dbContent. ("query here") return value ; }我可以在实体框架中完成吗? 可能吗?
I have a DbContext class and I'm using code first apporach in my application. I have few common standard tables that contains "Id" and "Value" columns and i want to query them passing the table name and column name but there is no option in entity framework to pass.
Example:
Common tables:
Client_ClientDepartment (Id, Value) Client_ClientDesignation (Id, Value) Client_ClientCompany (Id, Value)What I want to do is to pass table name and Id to get the value. I have created a common method as
public string GetClientValue(string id, string tableName) { DatabaseContext dbContext = new DatabaseContext(); //Query the database and get value based on table and id. string value = dbContent. ("query here") return value ; }Can I do it in entity framework? Is it possible?
最满意答案
using ( DatabaseContext dbContext = new DatabaseContext()) { var blogs = dbContext.Database.SqlQuery<>("query here").ToList(); } using ( DatabaseContext dbContext = new DatabaseContext()) { var blogs = dbContext.Database.SqlQuery<>("query here").ToList(); }更多推荐
发布评论