2

サーバークラスを他のクラスの子としてデータベースを作成しています。外部キーでの書き込みは正常に機能しますが、データを取り戻そうとするとエラーがスローされます:

タイプ 'SQLiteNetExtensions.Exceptions.IncorrectRelationshipException' の未処理の例外が SQLiteNetExtensions.dll で発生しました

追加情報: MatchDetail.timeline: OneToOne 関係の少なくとも 1 つのエンティティには外部キーが必要です

これは私の作成コードです:

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.Exceptions.IncorrectRelationshipException' の未処理の例外が SQLiteNetExtensions.dll で発生しました

追加情報: 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; }

外部キー名を明示的に宣言すると、リファクタリングの問題が発生する可能性があるため、厳密に要求されない限り、この方法をお勧めします。


もう 1 つの関係は として宣言されて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 拡張機能のドキュメントサンプル プロジェクトを参照してください。

于 2015-04-08T20:01:14.487 に答える