Reputation: 2347
I have a Sqlite database table named Movie whose columns basically store data simply in TEXT or INTEGER type (of course, Sqlite basically stores everything as just string the columns only have an affinity towards the type, if i am not wrong). I am using Linq-to-Sqlite ORM to query my table data against a model named Movie. Below is the DDL for the table and code for the class.
CREATE TABLE Movie
(
Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
Title Text,
Rating TEXT,
IsSubtitle INTEGER
)
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public double Rating { get; set; }
public bool IsSubtitle { get; set; }
}
Now when I try to fetch the movie records from the database using the ORM, it throws an exception :
System.InvalidOperationException was caught
HResult=-2146233079
Message=The 'Rating' property on 'Movie' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.Double'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at LinqToSqliteDemoApp.Program.Main(String[] args) in e:\Projects\TestApplications\LinqToSqliteDemoApp\LinqToSqliteDemoApp\Program.cs:line 25
InnerException:
Obviously, it cannot cast the TEXT type of data from the Rating column into the double data type of Movie class. So I would like to know, is there any workaround to tell the ORM to implicitly map or convert the Rating column data to double data type while retrieving from the database ?
Upvotes: 1
Views: 414
Reputation: 41759
You need to read the docs on sqlite data types http://www.sqlite.org/datatype3.html, so use double in your create table statement
Upvotes: 1