ServiceStack ORMLite - Select Columns

I recently started working with ServiceStack and its ORMLite platform. I searched on Google and looked at the source code, but could not find anything suitable.

Is there a way to select specific columns when executing a query with ORMLite? Something like this: Db.First<Model>(q => q.Id == someId, "Column1, Column2")

If I did not miss this function, I am surprised that no one asked about this before, as this is one of the main rules for optimizing database transactions.

+7
source share
4 answers

If you want to specify other columns that should use the table as in this previous example

So, in your case, you can do something like:

 Db.First<Model>("SELECT Column1, Column2 FROM AnyTableOrView"); 

You can also create a partial model that looks at your table by decorating it with the [Alias] attribute, for example:

 [Alias("AnyTableOrView")] public class Model { public int Id { get; set; } public string Column1 { get; set; } public string Column2 { get; set; } } 

Then you can do something like:

 Db.First<Model>(q => q.Id == someId); 

And it will only SELECT + fill in the fields from the partial model.

+7
source

I tried this:

  • VIEW database created (table name and columns already installed)
  • Created a class called "Event" and matched all the fields for this table with the property (I used [Alias] for the table name and had good names for all columns)
  • Wrote access to the database to select 1 record based on this ID

      var dbFactory = new OrmLiteConnectionFactory( "Data Source=MyDB;User Id=user;Password=pwd", // Connection String OracleDialect.Provider); using (var db = dbFactory.OpenDbConnection()) { var event = db.GetByIdOrDefault<Event>( request.Id ); } 

At this point, the var 'event is populated, but only the Id field is populated! all other fields of the class are not populated (although there really is data in the database).

This is the easiest way I can do, and it does not work. Any ideas? (PS: I am using OrmLite for Oracle)

thanks

+2
source

I found a problem. This was due to incorrect type matching between the field in my class (defined as a string) and the corresponding Oracle field (this is DATE).

I replaced the string with datetime and worked like a charm.

Thus, it works great with VIEW and that simplifies the code MORE.

+1
source

I had a similar problem, however my solution was different.

I had an int property in my POCO. My request (from Oracle) returned null for this property. This caused an exception to be thrown and prevented further processing of this line. As a result, POCO was partially filled.

The solution was to change the type to NULL.

 public int? mypropperty 
+1
source

All Articles