9

How exactly is ServiceStack OrmLite handling default and computed columns?

Specifically I'm getting the error

The column "PointsAvailable" cannot be modified because it is either a computed column or is the result of a UNION operator.

This column is configured as a computed column in an SQL Server 2008 database.

OrmLite does seem to do something with computed columns as you can add the attribute '[ServiceStack.DataAnnotations.Compute]' to a property in a model.

Stepping into the code, the function 'ToInsertRowStatement' in 'OrmLiteDialetBase.cs' is called. While that function is checking if the AutoIncrement property is set, it isn't checking if the IsComputed property is set.

I don't know if this is a bug or if I'm just using it wrong.

John
  • 1,286
  • 2
  • 14
  • 22
  • I would try adding a check for IsComputed. If that fixes the problem, create a pull request on github with the bugfix. ServiceStack is a well-architected program, so it's unlikely that there are multiple places where this check might happen. – theMayer Oct 15 '12 at 23:39
  • To clarify my previous comment, you would do this in the source code, then recompile the binary. Shouldn't take more than about 5 minutes. – theMayer Oct 16 '12 at 02:08

2 Answers2

6

For my computed columns which are only computed in the service layer, SQL knows nothing about them, so I used a combination of the following attributes on the servicestack model:

[Compute, ServiceStack.DataAnnotations.Ignore]
public List<MyModel> MyList {get;set;}

The difference seems to be the "Ignore" attribute which insisted upon having it's namespace attached ??. With these in place, my basic queries run, otherwise SQL complains that the columns don't exist - rightly enough!

You can, as suggested by t-clausen.dk use an SQL Filter by specifically passing an SQL CommandText string with all the column names you do want, but I think that opens a maintenance issue.

As for a bugfix that looks at the database, it appears the the SQL is generated on a per-provider basis by a "toSqlString()" or similar method. So there are probably a few spots to pay attention to...

EDIT: It is simply the Ignore attribute which does the job. From the source:

/// IgnoreAttribute  
/// Use to indicate that a property is not a field  in the table  
/// properties with this attribute are ignored when building sql sentences

There is also the option to use an ALIAS which I've not explored.

0

I surgest you create a view with the relevant columns (excluding the computed columns) from the table and work from the view instead. This way you avoid references to unwanted computed columns. Simple views can be treated the same way as normal tables with regard to insert, delete, update and most other aspects.

t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
  • That's kinda... hacky. I was hoping there was some inbuilt way in ServiceStack to do it. – John Oct 11 '12 at 08:33
  • @john it seems you are currently experiencing some sort of bug in ServiceStack OrmLite. Since I can't fix your tool, a workaround should be the second best thing, this is my surgestion to a workaround. Otherwise you can contact the people who made OrmLite and request a fix. – t-clausen.dk Oct 11 '12 at 09:09