在实体框架中获取没有约束的表的列名

时间:2016-10-04 12:22:54

标签: c# sql asp.net-mvc entity-framework

使用Entity Framework,如何才能使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; }

我使用下面的代码片段来获取列名,但它返回了Model

中的所有属性
var names = typeof(EmployeeMaster).GetProperties()
                    .Select(property => property.Name)
                    .ToArray();

1 个答案:

答案 0 :(得分:1)

试试这个:

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