将表名传递给dbContext并从Entity Framework中的表中获取值(Pass table name to dbContext and get values from table in E

编程入门 行业动态 更新时间:2024-10-24 14:23:24
将表名传递给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(); }

更多推荐

本文发布于:2023-08-04 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并从   Entity   将表名   dbContext   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!