c# – 如何将包含空格的列名映射到POCO属性?
发布时间:2020-12-15 23:45:24  所属栏目:百科  来源:网络整理 
            导读:我正在处理数据库表,其中列名称中包含空格,例如“级别描述”. 我无法更改列名称.现在我有一个这个表的实体框架模型类,编译器抱怨这个属性,因为属性名称不能包含空格! 如何在班级中定义带空格的列? [Table("StudyLevel")]public class StudyLevelModel{ [Ke
                
                
                
            | 
 我正在处理数据库表,其中列名称中包含空格,例如“级别描述”. 
  
  我无法更改列名称.现在我有一个这个表的实体框架模型类,编译器抱怨这个属性,因为属性名称不能包含空格! 如何在班级中定义带空格的列? [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; }
}(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
