テーブルの 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;