我正在使用服务器类作为其他类的子级来创建数据库。使用外键编写工作正常,但是当我尝试取回数据时会抛出错误:


SQLiteNetExtensions.dll中发生了类型为'SQLiteNetExtensions.Exceptions.IncorrectRelationshipException'的未处理异常

附加信息: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; }


有错误:


SQLiteNetExtensions.dll中发生了类型为'SQLiteNetExtensions.Exceptions.IncorrectRelationshipException'的未处理异常

附加信息:MatchDetail.participantIdentities:无法找到OneToMany关系的外键

最佳答案

您正在手动指定外键名为“ 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类中:

[ForeignKey(typeof(MatchDetail)]
public int matchDetailId { get; set; }


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

10-04 21:06