Skip to main content
 首页 » 编程设计

c#之当列实际存在于数据库中时,ServiceStack OrmLite 返回无效的 ColumnName 错误

2024年02月06日19lovecherry

我有一个如下所示的类:

public class EmployeeHistory 
    {        
        public int EmployeeHistoryId { get; set; }     
        public int TitleId { get; set; }     
        public int EmployeeId { get; set; }     
        public bool IsDeleted { get; set; }           
        public int GeographyId { get; set; }            
        public Geography Geography { get; set; }              
        public Title Title { get; set; }          
        public DateTime StartDate { get; set; }     
        public DateTime EndDate { get; set; } 
    } 

我正在尝试将其映射到如下所示的数据库表中:

CREATE TABLE [Data].[EmployeeHistory]( 
    [EmployeeHistoryId] [int] IDENTITY(1,1) NOT NULL, 
    [EmployeeId] [int] NOT NULL, 
    [GeographyId] [int] NULL, 
    [TitleId] [int] NULL, 
    [IsDeleted] [bit] NOT NULL, 
    [IsActive]  AS (case when [EndDate] IS NULL then (1) else (0) end), 
    [StartDate] [date] NOT NULL, 
    [EndDate] [date] NULL, 
    [CreatedDate] [datetime] NOT NULL, 
    [CreatedBy] [int] NOT NULL, 
    [ModifiedDate] [datetime] NOT NULL, 
    [ModifiedBy] [int] NOT NULL 

这里是问题的总结:ORMLite 能够正确序列化 EmployeeId、TitleId、EmployeeHistoryId但是它抛出“无效的列名” 尝试序列化 GeographyId 和 StartDate、EndDate 时出错。 我不确定它能够序列化的字段和它不能序列化的字段之间是否有任何区别。而且我以前从未遇到过 ORMLite 中的序列化问题。不确定这次我错过了什么?

只是添加一些细节:这也发生在我正在处理的另一个表上,即使该列清楚地存在于表中,它也拒绝识别该列并抛出“无效列名错误”

请您参考如下方法:

实际上,这对我来说有点无赖。事实证明,我所指的表属于数据库中的特定 Schema,默认情况下 ORMLite 在 DBO 模式中查找内容。然而,这次 Schema 是 DATA 而不是 Dbo,它开始失败,因为在 Dbo schema 中,它没有找到任何名为 EmployeeHistory 的东西。

我发现用 [Schema("SchemaName")] 属性装饰你的类很容易解决这个问题。