Linq-to-Sqlite:无法针对System.Double类型的模型属性映射TEXT类型的sqlite列?

时间:2015-03-25 19:44:07

标签: linq entity-framework sqlite

我有一个名为Movie的Sqlite数据库表,其列基本上只是以TEXT或INTEGER类型存储数据(当然,Sqlite基本上存储所有内容,只是字符串,列只对该类型有亲和力,如果我没有错)。 我正在使用Linq-to-Sqlite ORM来查询名为Movie的模型的表数据。下面是表的DDL和类的代码。

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

现在,当我尝试使用ORM从数据库中获取电影记录时,它会引发异常:

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: 

显然,它无法将Rating列中的TEXT类型的数据转换为Movie类的双数据类型。 所以我想知道,是否有任何解决方法告诉ORM在从数据库中检索时隐式地将Rating列数据映射或转换为double数据类型?

1 个答案:

答案 0 :(得分:1)

您需要阅读sqlite数据类型http://www.sqlite.org/datatype3.html上的文档,因此在create table语句中使用double