【问题标题】:Cascade on Delete/Nullable Foreign Key级联删除/可为空的外键
【发布时间】:2015-06-15 14:30:13
【问题描述】:

我使用现有数据库在 MVC 5 / EF 6 中创建了一个代码优先项目。我有两个表之间的一对多关系。 “WebLeads”表中的一条记录可以包含“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.

我尝试添加“删除时将级联”,但没有成功。据我了解,删除时的级联仅在外键不可为空时才有效。

     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 设计的新手,所以我猜我的问题在于模型构建器?

谢谢!

笔记模型

    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 模型

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

}

【问题讨论】:

  • 你能发布你的 WebLead 和 Notes 模型吗?
  • 由于您是先使用代码,您是否在更新模型后运行了 update-database 命令?
  • 顺便说一句:您说“我使用现有数据库在 MVC 5 / EF 6 中创建了代码优先项目”,然后说您首先使用代码。确定不先使用数据库吗?
  • 我创建了一个以现有数据库为模型的 Code first 项目。当您创建新项目时,MVC % 为您提供了该选项。我没有 EDMX 文件。
  • 您在修改模型后是否运行了 update-database?

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2020-10-30
    • 2012-09-21
    • 2011-02-24
    • 2018-04-13
    相关资源
    最近更新 更多