Iti Tyagi
Iti Tyagi

Reputation: 3661

Columns names from entity using LINQ

I was able to get the columns names by using this:

var props = typeof(FMCSA_NPR).GetProperties();

But it is also giving me the names of other tables which have a foreign relation with the specified table.

Is there a way by which I can only get the column names? What do we call column names when referring table as entity in Entity Model?

Upvotes: 0

Views: 73

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109080

You can list the non-navigation properties of entities by accessing the conceptual model (CSpace):

var oc = ((IObjectContextAdapter)db).ObjectContext;
var cs = oc.MetadataWorkspace.GetEntityContainer(oc.DefaultContainerName, 
                                                 DataSpace.CSpace);

foreach (var entitySet in cs.EntitySets)
{
    var props = string.Join(",", entitySet.ElementType.Properties);
    Trace.WriteLine(string.Format("{0}: {1}", entitySet.Name, props));
}

(Where db is your DbContext object).

Upvotes: 1

Related Questions