代码第一个一对一启用级联删除 [英] code first one-to-one enable cascade delete

查看:147
本文介绍了代码第一个一对一启用级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与外键有一对一的关系,但由于某些原因, Cascade Delete 未启用。示例代码如下。

  public class AppRegistration 
{
public int AppRegistrationId {get;组;
[必需]
[StringLength(50)]
[Display(Name =Username)]
public string UserName {get;组;
[必需]
[StringLength(100)]
public string Password {get;组; }
[StringLength(20)]
public string StudentOrAgent {get;组; }
//导航属性
public virtual AppStatus AppStatus {get;组; }
公共虚拟协议协议{get;组; }
public virtual AnotherTable AnotherTable {get;组; }
}

具有外键的从属表位于下面。

  public class协议
{
[Key]
[ForeignKey(AppRegistration)]
public int AppRegistrationId {get;组; }
public DateTime DateAgreed {get;组; }
public virtual AppRegistration AppRegistration {get;组; }
}

当我尝试从生成的 AppRegistrations table我得到一个引用约束冲突。



我尝试在依赖表格的导航属性中放入 [必需] ,但不执行任何操作 - Update-Database 命令显示不等待基于代码的迁移。消息。有任何想法吗?谢谢。



更新:
我收到以下错误消息:



DELETE语句与REFERENCE约束FK_dbo.AppStatus_dbo.AppRegistrations_AppRegistrationId冲突。冲突发生在数据库MVCapp,表dbo.AppStatus,AppRegistrationId列。

解决方案

我决定在单独的示例项目中制定级联删除问题。我发现以下博客MSDN页面非常有用。





  • I have one to one relationship with foreign keys but the Cascade Delete is not enabled for some reason. The sample code is below.

    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; }
    }
    

    The dependent table with a foreign key is below.

    public class Agreement
    {
        [Key]
        [ForeignKey("AppRegistration")]
        public int AppRegistrationId { get; set; }
        public DateTime DateAgreed { get; set; }
        public virtual AppRegistration AppRegistration { get; set; }
    }
    

    When I try to delete an entry from the generated AppRegistrations table I get a Reference constraint conflict.

    I tried putting [Required] on the navigation property in the dependent table but it doesn't do anything - the Update-Database command shows the No pending code-based migrations. message. Any ideas? Thanks.

    Update: I'm getting the following error message:

    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'.

    解决方案

    I decided to work out the cascade delete problem in a separate sample project. I found the following blog & MSDN pages very useful.

    Using the Code First approach create the following Model.

    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public virtual Book Book { get; set; }
    }
    public class Book
    {
        public int CategoryId { get; set; }
        public string BookTitle { get; set; }
        public string BookAuthor { get; set; }
        public string BookISBN { get; set; }
        public virtual Category Category { get; set; }
    }
    

    (I realize the entity names suggest one-to-many relationship, but I am trying to model 1-to-1 relationship, as in my original question at the top.)

    So, in the above model each Category can only have one Book.

    In your DbContext-derived class add the following.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
        modelBuilder.Entity<Book>()
            .HasKey(t => t.CategoryId);
    
        modelBuilder.Entity<Category>()
            .HasRequired(t => t.Book)
            .WithRequiredPrincipal(t => t.Category)
            .WillCascadeOnDelete(true);
    }
    

    (The following namespaces are required for the above code: System.Data.Entity, System.Data.Entity.ModelConfiguration.Conventions.)

    This properly creates the 1-to-1 relationship. You'll have a primary key in each table and also a foreign key in Book table with ON DELETE CASCADE enabled.

    In the above code, on the Category entity I used WithRequiredPrincipal() with t => t.Category argument, where the argument is the foreign key column in the dependent table.

    If you use WithRequiredPrincipal() without an argument you'll get an extra column in the Book table and you'll have two foreign keys in the Book table pointing to CategoryId in Category table.

    I hope this info helps.

    UPDATE

    Later on I found answer directly here:

    http://msdn.microsoft.com/en-us/data/jj591620#RequiredToRequired

    这篇关于代码第一个一对一启用级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆