次のクラスに対して、EFコードを使用して1対1の関係を構成しようとしています。バスはプリンシパルクラスであり、スケジュールは従属クラスです。1対1リレーションシップコード最初にEF規則に従わない
public class Bus
{
public int Id { get; set; }
public virtual Schedule Schedule { get; set; }
}
public class Schedule
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime BusRouteStartTime { get; set; }
public DateTime BusRouteEndTime { get; set; }
public DateTime ArrivalTime { get; set; }
public DateTime DepartureTime { get; set; }
[ForeignKey("Bus")]
public int BusId { get; set; }
public virtual Bus Bus { get; set; }
}
このクラスはthisチュートリアルを使用して作成しました。私はPCMで、マイグレーションを有効にしないと私はこのエラーを取得する場合:
Bus_Schedule_Target: : Multiplicity is not valid in Role 'Bus_Schedule_Target' in relationship 'Bus_Schedule'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
私はこのエラーをGoogleとthis解決策を見つけます。私はOnModelCreating()を変更します:
modelBuilder.Entity<Schedule>().HasKey(s => s.Id);
modelBuilder.Entity<Bus>()
.HasRequired(s => s.Schedule)
.WithRequiredPrincipal(b => b.Bus);
残念ながら、それは助けになりませんでした。私は、デフォルトでは依存クラスのIDがプライマリと外部キーでなければならないことを同時に理解するとGoogleに続ける。それは後でドロップテーブルで問題を引き起こす可能性があるため、カスケード削除などの理由で私には合いません。
私はそれが何らかの重複した質問であることを理解していますが、このようにすることができれば本当に良いでしょう。