私は外部キーと1対1の関係を持っていますが、何らかの理由でCascade Delete
が有効になっていません。サンプルコードは以下のとおりです。コードfirst one-to-one enableカスケード削除
public class AppRegistration
{
public int AppRegistrationId { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[StringLength(100)]
public string Password { get; set; }
[StringLength(20)]
public string StudentOrAgent { get; set; }
// navigation properties
public virtual AppStatus AppStatus { get; set; }
public virtual Agreement Agreement { get; set; }
public virtual AnotherTable AnotherTable { get; set; }
}
外部キーを持つ従属テーブルは以下のとおりです。
public class Agreement
{
[Key]
[ForeignKey("AppRegistration")]
public int AppRegistrationId { get; set; }
public DateTime DateAgreed { get; set; }
public virtual AppRegistration AppRegistration { get; set; }
}
生成されたAppRegistrations
テーブルからエントリを削除しようとすると、参照制約の競合が発生します。
私は従属テーブルにナビゲーションプロパティに[Required]
を入れてみましたが、それは何もしません - Update-Database
コマンドはNo pending code-based migrations.
メッセージが表示されます。何か案は?ありがとう。
アップデート:私は、次のエラーメッセージが出てい :
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.AppStatus_dbo.AppRegistrations_AppRegistrationId". The conflict occurred in database "MVCapp", table "dbo.AppStatus", column 'AppRegistrationId'.
感謝。私は '[Required]'を入れてみましたが、コードマイグレーションの 'Update-Database'は' No pending code-based migrations'を返します。これを回避する方法はありますか? – erdinger
私は間違っているかもしれませんが、両方向で必要とされる一対一の関係を満たすことはほとんど不可能であるため、最初の手法を試したくないと思います。契約を挿入しようとすると、AppRegistrationが存在しないために制約違反が発生し、その逆もあります。 – AaronLS
@ user2291748 update-databaseを実行する前に移行を追加しましたか? AaronLS、おそらくあなたが正しいと思うかもしれません。 – MattSull