Skip to content Skip to sidebar Skip to footer

How Can I Get Data Type Of Each Column In A Sql Server Table/view Etc. Using C# Entity Framework?

How can I get the type of each column in a SQL Server table or view using Entity Framework? I need to do this BEFORE I get all the data from the table, because I want to allow us

Solution 1:

you can use the GetProperty and then PropertyType:

using (var ArgoEntities = new ARGOEntities())
        {
            //find the types here before the user performs the query so i can build the below code//Like this you can retrieve the types:foreach (string propertyName in ArgoEntities.CurrentValues.PropertyNames)
            {
                   var propertyInfo = ArgoEntities.Entity.GetType().GetProperty(propertyName);
                   var propertyType = propertyInfo.PropertyType;

            }
            //var query = from b in ArgoEntities.tbl_Books
                        where b.PublishDate>[user specified date]  //some date here the user entersselect b;

            var book = query.First();
        }

Solution 2:

Ryios' comment lead me to the extremely simple answer I knew it had to be, which will give me an array of PropertyInfo for each field in the table.

var columns=typeof(tbl_Books).GetProperties();

Post a Comment for "How Can I Get Data Type Of Each Column In A Sql Server Table/view Etc. Using C# Entity Framework?"