如何通过在MVC中传递表名来获取列名 [英] How to get column names by passing table name in MVC

查看:90
本文介绍了如何通过在MVC中传递表名来获取列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想通过使用实体框架在mvc中传递表名来显示列名。

并使用下面的查询获取单个表的列名(管理员)。

如何在实体框架中动态传递tablename。

请给我解决方案。



我尝试过:



Hi all,
I want to display column names by passing table name in mvc using entity framework.
and by using below query getting column names for single table(Administrator) only.
How to pass tablename dynamically in entity framework.
please give me solution regarding this.

What I have tried:

public List<string> GetColumnNames(string tablename)
        {
            using (var Context = new MCPEntities())
            {
                var names = typeof(Administrator).GetProperties().Select(property => property.Name).ToArray();
                return names.ToList();              
            }

推荐答案

使用原始SQL查询获取列名



实体框架原始SQL查询 [ ^ ]



如何从SQL Server中的表中获取列名? - 堆栈溢出 [ ^ ]



在实体框架中,不必在EF之间进行一对一的映射实体和表名,所以查询实体的名称不一定得到表名,也不一定得到原始名称。
Use a raw SQL query to get the column names

Entity Framework Raw SQL Queries[^]

How can I get column names from a table in SQL Server? - Stack Overflow[^]

In Entity Frame there doesn't have to be a one to one mapping between EF entities and table names so querying the names of entities doesn't necessarily get you the table names, nor will it get the raw ones.


我同意F-ES Sitecore,有可以表实体属性和实际表字段之间的阻抗。您希望显示的内容取决于您。如果要显示实际的数据库表字段,请使用F-ES Sitecore建议的解决方案。但一般而言,表字段名称约定遵循基于组织策略的典型模式,并且通常不应按原样在UI中显示,而是使用更具描述性的文本。这样,您的方法就足够了。
I agree with F-ES Sitecore, that there can be impedance between table entity properties and actual table fields. It is up to you on what you exactly want to display. If you want to display the actual database table fields, then use the solution what is suggested by F-ES Sitecore. But in general, table field name conventions follow a typical pattern based on the organization's policies, and typically are not supposed to display in UI as is, but with more descriptive text. In that way, your approach can be sufficient.


这篇关于如何通过在MVC中传递表名来获取列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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