Search code examples
c#servicestackormlite-servicestack

ServiceStack ORMLite support for Views


I have read mythz's post here about how ORMLite can read anything up from SQL and fit it into a POCO of the same shape. That is great.

On the other hand, how does ORMLite handle these "View POCOs" when saving them back into the database? Since they are not tables, they may be views or they may be just any sql select queries like that:

var rows = dbCmd.Select<ShipperTypeCount>(
     "SELECT ShipperTypeId, COUNT(*) AS Total FROM Shippers GROUP BY ShipperTypeId ORDER BY COUNT(*)");

Solution

  • There's nothing special about the POCOs you use with OrmLite, they're not tied or related back to any underlying tables and there's no hidden magic state that OrmLite caches in between calls so it knows what fields map to.

    With every DB call OrmLite just uses the POCO to create the appropriate SELECT, INSERT, UPDATE or DELETE statement based on the schema definition of the type. The INSERT Apis shows some examples of this.

    It's best to think of OrmLite as just turning your POCO into an SQL statement, which is what it does. So trying to insert a ShipperTypeCount will attempt to insert a record into a table called ShipperTypeCount unless it has an [Alias("UseTableNameInstead")] attribute which it will use instead.