Tox*_*xic 3 c# entity-framework
我正在处理数据库表,其中列名称中包含空格,例如"Level Description".
我无法更改列名称.现在我有一个这个表的实体框架模型类,编译器抱怨这个属性,因为属性名称不能包含空格!
如何在班级中定义带空格的列?
[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; }
}
Run Code Online (Sandbox Code Playgroud)
使用ColumnAttribute设置名称:
[Column(Name="Level Description")]
public string LevelDescription { get; set; }
Run Code Online (Sandbox Code Playgroud)
你不需要有自己的模型的属性名称完全匹配您的表的列名; [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; }
}
Run Code Online (Sandbox Code Playgroud)