Sqlite.net 扩展,无法使用 GetWithChildren 获取数据
Sqlite.net extentions, Cant get data with GetWithChildren
我正在创建一个数据库,其中包含几个 class 和 children 个其他 class 个。使用外键写入工作正常但是当我尝试取回数据时它会抛出错误:
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; }
}
其他 class 只是一个 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; }
并将MatchDetail
的外键添加到ParticipantIdentity
class:
[ForeignKey(typeof(MatchDetail)]
public int matchDetailId { get; set; }
查看 SQLite-Net 扩展 documentation and sample project 以获得更多示例。
我正在创建一个数据库,其中包含几个 class 和 children 个其他 class 个。使用外键写入工作正常但是当我尝试取回数据时它会抛出错误:
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; }
}
其他 class 只是一个 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; }
并将MatchDetail
的外键添加到ParticipantIdentity
class:
[ForeignKey(typeof(MatchDetail)]
public int matchDetailId { get; set; }
查看 SQLite-Net 扩展 documentation and sample project 以获得更多示例。