我正在处理数据库表,该表的列名中有一个空格,例如“Level Description”。
我无法更改列名。现在我有这个表的 Entity Framework 模型类,编译器提示这个属性,因为属性名称不能包含空格!
如何在我的类中定义带空格的列?
[Table("StudyLevel")]
public class StudyLevelModel
{
[Key]
public byte StudyLevelID { get; set; }
// How to map this to the column "Level Description"?
public string Level Description { get; set; }
public string SLevelType { get; set; }
public Nullable<bool> IsActive { get; set; }
public string ESID { get; set; }
public string RID { get; set; }
public byte LevelSearchGroup { get; set; }
}
最佳答案
您不必让您的模型属性名称与您的表列名称完全匹配;您可以应用 [Column]
属性将属性映射到列:
[Table("StudyLevel")]
public class StudyLevelModel
{
[Key]
public byte StudyLevelID { get; set; }
[Column("Level Description")]
public string LevelDescription { get; set; }
public string SLevelType { get; set; }
public Nullable<bool> IsActive { get; set; }
public string ESID { get; set; }
public string RID { get; set; }
public byte LevelSearchGroup { get; set; }
}
关于c# - 如何将其中包含空格的列名称映射到 POCO 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049620/