gpt4 book ai didi

Sqlite.net 扩展,无法使用 GetWithChildren 获取数据

转载 作者:行者123 更新时间:2023-12-03 17:48:05 32 4
gpt4 key购买 nike

我正在创建一个数据库,其中多个类作为其他类的子类。使用外键编写工作正常,但是当我尝试取回数据时,它会引发错误:

An unhandled exception of type 'SQLiteNetExtensions.Exceptions.IncorrectRelationshipException' occurred in SQLiteNetExtensions.dll

Additional information: MatchDetail.timeline: At least one entity in a OneToOne relationship must have Foreign Key



这是我的创建代码:
SQLiteConnection db = new SQLiteConnection(new SQLite.Net.Platform.Win32.SQLitePlatformWin32(), "Matches.db3");
db.CreateTable<ParticipantIdentity>();
db.CreateTable<MatchDetail>();
db.CreateTable<Timeline>();
db.InsertWithChildren(md, true);
var m = db.GetWithChildren<MatchDetail>(matchId, true);

我的类(class):
[Table("Matches")]
public class MatchDetail
{
[PrimaryKey]
public long matchId { get; set; }
public int mapId { get; set; }
[JsonConverter(typeof(DateTimeConverterFromLong))]
public DateTime MatchCreation { get; set; }
public long matchDuration { get; set; }
public MatchMode matchMode { get; set; }
public MatchType matchType { get; set; }
public string matchVersion { get; set; }

public string platformId { get; set; }
public Queuetype queueType { get; set; }
public Region region { get; set; }
public Season season { get; set; }

[ForeignKey(typeof(Timeline), Name = "TimelineId"), Indexed]
public int timelineId { get; set; }
[OneToOne("TimelineId", CascadeOperations = CascadeOperation.All)]
public Timeline timeline { get; set; }

[ForeignKey(typeof(ParticipantIdentity), Name = "ParticipantId"), Indexed]
public int participantIdentitiesId { get; set; }
[ManyToOne("ParticipantId", CascadeOperations = CascadeOperation.All)]
public List<ParticipantIdentity> participantIdentities { get; set; }
}

其他类只是一个 id 和一些其他基本类型,我一直在努力解决这个问题,但它就是不想工作。

编辑:
[ForeignKey(typeof(ParticipantIdentity))]
public int participantIdentitiesId { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<ParticipantIdentity> participantIdentities { get; set; }

有错误:

An unhandled exception of type 'SQLiteNetExtensions.Exceptions.IncorrectRelationshipException' occurred in SQLiteNetExtensions.dll

Additional information: MatchDetail.participantIdentities: Unable to find foreign key for OneToMany relationship

最佳答案

您手动指定外键名为“TimelineId”,但实际上外键名为“timelineId”(注意首字母大写)。

改变这个:

[ForeignKey(typeof(Timeline), Name = "TimelineId"), Indexed]
public int timelineId { get; set; }
[OneToOne("TimelineId", CascadeOperations = CascadeOperation.All)]
public Timeline timeline { get; set; }

对此:
[ForeignKey(typeof(Timeline)]
public int timelineId { get; set; }
[OneToOne(CascadeOperations = CascadeOperation.All)]
public Timeline timeline { get; set; }

显式声明外键名称可能会导致重构问题,因此除非严格要求,否则这是推荐的方式。

另一个关系被声明为 ManyToOne但它实际上是 OneToMany .要使其工作,您必须更改属性类型并将外键移动到另一端。

改变关系:
[ForeignKey(typeof(ParticipantIdentity), Name = "ParticipantId"), Indexed]
public int participantIdentitiesId { get; set; }
[ManyToOne("ParticipantId", CascadeOperations = CascadeOperation.All)]
public List<ParticipantIdentity> participantIdentities { get; set; }

有了这个:
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<ParticipantIdentity> participantIdentities { get; set; }

并将外键添加到 MatchDetailParticipantIdentity类(class):
[ForeignKey(typeof(MatchDetail)]
public int matchDetailId { get; set; }

查看 SQLite-Net 扩展 documentationsample project更多示例。

关于Sqlite.net 扩展,无法使用 GetWithChildren 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521044/

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