Lost
Lost

Reputation: 13575

ServiceStack OrmLite returing with Invalid ColumnName error when column actually exists in the database

I have a class which looks like following:

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; }
    }

and I am trying to map it with a DB table which looks like following:

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

Here is the summary of the problem: ORMLite is able to serialize EmployeeId, TitleId, EmployeeHistoryId correctly BUT it throws "Invalid Column Name" error when it tries to serialize GeographyId and, StartDate, EndDate. I am not sure if there is any difference between the fields which it is able to serialize and the fields that it is not able to serialize. And also I have never had a problem with serialzation in ORMLite before. Not sure what am I missing this time around?

Just to add some Details: This also happened to another table that I am working on and again even though the column clearly exists in the table, it refuses to recognize the column and throws the "Invalid column name error"

Upvotes: 1

Views: 711

Answers (2)

Hung Vu
Hung Vu

Reputation: 5914

Make sure you didn't ignore that field in the ModelDefinition of OrmLite. Below is part of a method I wrote to Ignore Field definition.

 ModelDefinition definition = GetModelDefinition(typeof(T));

        if (definition == null)
            return this;

        string fieldName = selector.GetPropertyName();
        FieldDefinition field = definition.GetFieldDefinition(fieldName);

        if (field != null) {
            definition.FieldDefinitions.Remove(field);
            definition.IgnoredFieldDefinitions.Add(field);
            definition.AfterInit();
        }

Upvotes: 2

Lost
Lost

Reputation: 13575

Actually, this was a little bummer from my side. It turned out that the table that I am referring to belongs to a particular Schema in the DB and by default ORMLite looks for things in the DBO schema. However, this time Schema was DATA instead of Dbo and it started failing because in Dbo schema, it did not find anything called EmployeeHistory.

I found that it is easily addressed by decorating your class with [Schema("SchemaName")] attribute.

Upvotes: 2

Related Questions