Click here to Skip to main content
15,911,317 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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();              
            }
Posted
Updated 9-Jun-17 19:54pm
v5

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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900