2

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

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 关系的外键

4

1 回答 1

4

您手动指定外键名为“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; }

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


另一个关系被声明为 aManyToOne但它实际上是 a 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 Extensions文档示例项目以获取更多示例。

于 2015-04-08T20:01:14.487 回答