我正在创建一个包含多个类的数据库作为其他类的子类。使用外键写入工作正常但是当我尝试获取数据时它会抛出错误:
未处理的类型' SQLiteNetExtensions.Exceptions.IncorrectRelationshipException'发生在SQLiteNetExtensions.dll
中其他信息:MatchDetail.timeline:OneToOne关系中至少有一个实体必须具有外键
这是我的创作代码:
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);
我的班级:
[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; }
有错误:
未处理的类型&#39; SQLiteNetExtensions.Exceptions.IncorrectRelationshipException&#39;发生在SQLiteNetExtensions.dll
中其他信息:MatchDetail.participantIdentities:无法找到OneToMany关系的外键
答案 0 :(得分:3)
您正在手动指定外键被命名为&#39; TimelineId&#39;但实际上,外键被命名为“timelineId”&#39; (注意第一个字母的大写)。
改变这个:
[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
类:
[ForeignKey(typeof(MatchDetail)]
public int matchDetailId { get; set; }
有关更多示例,请查看SQLite-Net Extensions documentation和sample project。