2

テーブルの 1 つが 1 対 1 の関係と多対多の関係を持つデータベースを作成しようとしています。ここに私のモデルがあります:

public class Trip
    {
        public int ID { get; set; }
        public virtual ICollection<Place> PassingBy { get; set; }
        public Place Origin { get; set; }
        public Place Destination { get; set; }
        public DateTime Time { get; set; }
        public int EmptySlots { get; set; }
        public virtual ICollection<Person> Attendants { get; set; }
        public string AccessKey { get; set; }

    }

    public class Person
    {
        public int ID { get; set; }
        public string Username { get; set; }
        public virtual ICollection<Trip> Trips { get; set; }
    }

    public class Place
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Trip> Trips { get; set; }
    }


    public class GARDB : DbContext
    {
        public DbSet<Trip> Trips { get; set; }
        public DbSet<Person> Persons { get; set; }
        public DbSet<Place> Places { get; set; }
    }

今、移行を追加してデータベースを更新すると。データベースには、多対多の関係に必要な「PersonTrip」というテーブルがあります。しかし、 "PlaceTrip" のテーブルはありません。いくつかの試行錯誤の後。次の行が原因であることがわかりました。

    public Place Origin { get; set; }
    public Place Destination { get; set; }

それらは関係を 1 対多にします。私がなりたいと思うほど多対多ではありません。Place-Trip 関係を Person-Trip のようなものにする方法はありますが、これらの 2 行を削除する必要はありません。

ありがとう!

編集:解決策

私は自分が何を望んでいるのかを明確にしませんでした。一線を画したかった

public virtual ICollection<Place> PassingBy { get; set; }

行にマッピングされます

public virtual ICollection<Trip> Trips { get; set; }

プレイスクラスで。回答から得た解決策は次のとおりです。

交換 :

 public virtual ICollection<Place> PassingBy { get; set; }

と:

[InverseProperty("Trips")]
public virtual ICollection<Place> PassingBy { get; set; }

と置き換えます:

public virtual ICollection<Trip> Trips { get; set; }

Place クラスで:

[InverseProperty("PassingBy")]
public virtual ICollection<Trip> Trips { get; set; }

これを先頭に追加します:

using System.ComponentModel.DataAnnotations.Schema;
4

2 に答える 2

1

それを混乱させるのは、事実上、Tripの関係には2つの目的があるということです。

public Place Origin { get; set; }
public Place Destination { get; set; }

(これも仮想である必要があります、ところで)、そしてプレイスに1つだけ:

public virtual ICollection<Trip> Trips { get; set; }

...したがって、Entity Frameworkは、どちらが何に対応するかを理解できません。

Placeに2番目のナビゲーションプロパティといくつかのデータ注釈属性の両方を追加して、多対多の関係の2つの半分を形成するためにそれらがどのように関連するかをEntityFrameworkに指示する必要があります。次のように、明確にするために他のプロパティとコンテキストを省略します。

public class Trip
{
    [InverseProperty ("AsOrigin")]
    public virtual Place Origin { get; set; }

    [InverseProperty ("AsDestination")]
    public virtual Place Destination { get; set; }
}

public class Place
{
    [InverseProperty ("Origin")]
    public virtual ICollection<Trip> AsOrigin { get; set; }

    [InverseProperty ("Destination")]
    public virtual ICollection<Trip> AsDestination { get; set; }
}

出発地または目的地のいずれかとして、関係するすべての旅行を提供するPlaceの単一のプロパティが必要な場合は、次のようなナビゲーションプロパティを使用してプロパティを再作成できます。

[NotMapped]
public ICollection<Trip> Trips
{
    get
    {
        return AsOrigin.Concat(AsDestination).Distinct();
    }
}
于 2013-03-19T14:55:59.737 に答える
0

私はあなたがここで作成している関係をあまり好きではありませんが、DBA が噛み砕くべきものです。それまでの間、このようなことをしたい場合は、非常に似たようなことをしようとしているこの SO の質問への回答を利用することをお勧めします。さらにいくつかのプロパティを追加し、テーブル間の外部キー関係を設定する必要がありますが、問題なく動作するはずです。

于 2013-03-19T13:27:51.100 に答える