Linq到Sqlite:无法将文本类型Sqlite列映射到系统。模型属性的双重类型?
我有一个名为Movie的Sqlite数据库表,其列基本上以文本或整数类型存储数据(当然,Sqlite基本上以字符串形式存储所有内容。如果我没有错的话,这些列只与该类型有关联)。 我正在使用Linq到Sqlite ORM根据名为Movie的模型查询表数据。下面是表的DDL和类的代码Linq到Sqlite:无法将文本类型Sqlite列映射到系统。模型属性的双重类型?,linq,entity-framework,sqlite,Linq,Entity Framework,Sqlite,我有一个名为Movie的Sqlite数据库表,其列基本上以文本或整数类型存储数据(当然,Sqlite基本上以字符串形式存储所有内容。如果我没有错的话,这些列只与该类型有关联)。 我正在使用Linq到Sqlite ORM根据名为Movie的模型查询表数据。下面是表的DDL和类的代码 CREATE TABLE Movie ( Id integer NOT NULL PRIMARY KEY AUTOINCREMENT, Title Text, Rating TEXT,
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列中的文本类型的数据强制转换为Movie类的双重数据类型。
所以我想知道,在从数据库检索时,是否有任何解决方法可以告诉ORM隐式映射或将评级列数据转换为double数据类型?您需要阅读有关sqlite数据类型的文档,因此在create table语句中使用double肯定不会将所有内容都存储为字符串。它以自己的数据类型(TEXT/INTEGER/等)存储内容,但在单个列中支持混合类型,并在某些情况下执行自动强制。