我正在處理 EF 專案,并嘗試添加兩個外鍵,但是在添加遷移時遇到問題。
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public DateTime? DeathDate { get; set; }
public int? FatherId { get; set; }
public int? MotherId { get; set; }
[ForeignKey("FatherId")]
public virtual Person Father { get; set; }
[ForeignKey("MotherId")]
public virtual Person Mother { get; set; }
}
uj5u.com熱心網友回復:
在您的背景關系中添加此代碼
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasOptional(a => a.Mother)
.WithMany()
.HasForeignKey(a => a.MotherId);
modelBuilder.Entity<Person>()
.HasOptional(a => a.Father)
.WithMany()
.HasForeignKey(a => a.FatherId);
}
```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/390089.html
