我的學生表中有現有數據。現在我想在其中添加外鍵。當我嘗試使用現有數據添加外鍵時,它會給出錯誤「ALTER TABLE語句與FOREIGN KEY約束衝突」。如果我從Student表中刪除所有數據,那麼它會成功添加外鍵。現在我想添加外鍵而不會丟失遠程服務器上的數據。我該怎麼做?我看到了各種答案,但沒有找到解決辦法。當我在遠程服務器上的表中存在現有數據時,在我的表中添加外鍵?
學生表(見最後兩個屬性)
public int Id { get; set; }
[Required]
[StringLength(30)]
[RegularExpression(@"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$",
ErrorMessage = "Invalid name. Use letters only")]
public string Name { get; set; }
[Required]
[StringLength(30)]
[RegularExpression(@"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$",
ErrorMessage = "Invalid name. Use letters only")]
[Display(Name = "Father Name")]
public String FatherName { get; set; }
[Display(Name = "Student Picture")]
public String ImageURL { get; set; }
public Class Class { get; set; }
[Required]
[Display(Name = "Class Title")]
public int ClassId { get; set; }
[ForeignKey("Account")]
public int AccountId { get; set; }
public virtual Account Account { get; set; }
public string Gender { get; set; }
public string Status { get; set; } = "Active";
[Required]
[Range(0, 10000)]
public int RegistrationFee { get; set; }
public int InstalmentId { get; set; }
public virtual Instalment Instalment { get; set; }
}
這裏是我的分期付款表,標識我想讓FK在學生
public int Id { get; set; }
[Required]
public int Instalment1 { get; set; }
[Required]
public DateTime Date1 { get; set; }
public int Instalment2 { get; set; }
public DateTime Date2 { get; set; }
public int Instalment3 { get; set; }
public DateTime Date3 { get; set; }
public int Instalment4 { get; set; }
public DateTime Date4 { get; set; }
}
我想很簡單的解決方案因爲將來會有很多情況下我會相應地改變數據庫。謝謝!
請發佈兩個表的表模式。 – Eli