删除/可空外键上的级联

时间:2015-04-10 01:23:52

标签: c# sql-server asp.net-mvc entity-framework

我使用现有数据库在MVC 5 / EF 6中创建了第一个代码项目。我在两个表之间有一对多的关系。在" WebLeads"中的记录table可以在" Notes"中包含许多记录。表。

问题在于,当我创建数据模型时,我没有注意到Notes表中的LeadID外键允许空值。现在,当我尝试删除WebLeads中的记录时,我收到以下错误(如果Notes表中有相关​​记录)。

    The DELETE statement conflicted with the REFERENCE constraint "FK_Notes_WebLead". The conflict occurred in database "databasename", table "dbo.Notes", column 'LeadID'.
    The statement has been terminated.

我尝试在删除"上添加" Will Cascade,但它没有用。根据我的理解,删除级联只有在外键不可为空时才有效。

     modelBuilder.Entity<WebLead>()
    .HasMany(e => e.Notes)
    .WithOptional(e => e.WebLead)
    .HasForeignKey(e => e.LeadID)
    .WillCascadeOnDelete(true);

因此,通过SQL Management Studio,我将FK LeadID更改为不允许空值,并更新Notes模型中的代码,如下所示:

   public int LeadID { get; set; } //removed the int?

当我尝试构建项目时,对WebLeads表的第一个查询会引发以下错误:

    One or more validation errors were detected during model generation:

    Project.Models.WebLead_Notes: : Multiplicity conflicts with the referential constraint in Role 'WebLead_Notes_Source' in relationship 'WebLead_Notes'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

我还需要在Code First中做些什么才能让项目识别WebLeads和Notes表之间关系的变化?我采取了正确的方法吗?

因为Note表中的每条记录都需要一个LeadID,所以最好的办法是要求使用LeadID外键......但我似乎缺少一步或者两步才能使其工作。我是Code First设计的新手,所以我猜我的问题在于模型构建器吗?

谢谢!

Notes Model

    namespace Project.Models

public partial class Note
{
    public int NoteID { get; set; }

    public int LeadID { get; set; }

    public string NoteText { get; set; }

    [StringLength(50)]
    public string NoteBy { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? NoteDate { get; set; }

    public virtual WebLead WebLead { get; set; }
}

}

WebLeads Model

   namespace Project.Models
   {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class WebLead
{
    public WebLead()
    {
        Notes = new HashSet<Note>();
    }

    [Key]
    public int LeadID { get; set; }

    [Required]
    [StringLength(35)]
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [Required]
    [StringLength(15)]        
    public string Phone { get; set; }

    [Required]
    [StringLength(75)]
    public string Email { get; set; }

    [Required]
    [StringLength(20)]
    public string County { get; set; }


    [Column(TypeName = "datetime2")]
    [DisplayName("Lead Date")]
    public DateTime? LeadDate { get; set; }

    [StringLength(35)]
    [DisplayName("Lead Status")]
    public string LeadStatus { get; set; }

    public virtual ICollection<Note> Notes { get; set; }
}

}

1 个答案:

答案 0 :(得分:0)

我使用下面的链接找到了这个问题的答案。我不得不单独删除笔记表中的记录。

How to delete multiple records with Entity Framework ASP.Net MVC 5?

db.Notes.RemoveRange(db.Notes.Where(c => c.LeadID == id));