dinesh sns dinesh sns - 2 months ago 7
SQL Question

getting a table's column name without constraints in Entity Framework

Using Entity Framework, how can i get SQL table column names only belongs to that table i.e without its foreign key constraints?

Here is my model:

public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Nullable<byte> Age { get; set; }
public string Address { get; set; }
public string Gender { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EmployeeQualification> EmployeeQualifications { get; set; }


I have used the below snippet to get the column names but it returns all the properties in Model

var names = typeof(EmployeeMaster).GetProperties()
.Select(property => property.Name)
.ToArray();

Answer

Try this:

var names = typeof(EmployeeMaster).GetProperties()
              .Where(x => x.PropertyType.IsValueType || x.PropertyType == typeof(string))
              .Select(property => property.Name)
              .ToArray();