gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:11:09 25 4
gpt4 key购买 nike

我有一个名为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:


显然,它无法将“等级”列中的TEXT类型数据转换为Movie类的double数据类型。
因此,我想知道,有什么解决方法可以告诉ORM在从数据库中检索时隐式映射或将Rating列数据转换为double数据类型吗?

最佳答案

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

关于linq - Linq-to-Sqlite:无法针对System.Double类型的模型属性映射TEXT类型的sqlite列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com