2016-10-04 71 views
1

使用實體框架,如何獲得SQL表的列名只屬於該表,即沒有外鍵約束?實體框架中獲取表的列名不受約束

這裏是我的模型:

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; } 

我用下面的代碼片段,以獲得列名,但它在型號

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

回答

1

試試這個返回的所有屬性:

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

請記住,這將忽略所有映射,因此您的表格可能看起來完全不同。 – DevilSuichiro