我与外键有一对一的关系,但由于Cascade Delete
某种原因没有启用。示例代码如下。
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'.