Note that there are some explanatory texts on larger screens.

plurals
  1. POException Handling in EF when Try To Delete Not Cascaded Entity
    primarykey
    data
    text
    <p>assume that there are two entity:</p> <pre><code>public class Category { public string Id { get; set; } public string Caption { get; set; } public string Description { get; set; } public virtual IList&lt;Product&gt; Products { get; set; } } public class Product { public string Id { get; set; } public string CategoryId { get; set; } public string Caption { get; set; } public string Description { get; set; } public virtual Category Category { get; set; } } </code></pre> <p>and the cascade delete are not allowed.</p> <pre><code>public class ProductMap : EntityTypeConfiguration&lt;Product&gt; { public ProductMap() { // Primary Key this.HasKey(t =&gt; t.Id); // Properties this.Property(t =&gt; t.Caption) .IsRequired() .HasMaxLength(50); // Table &amp; Column Mappings this.ToTable("Products"); this.Property(t =&gt; t.Id).HasColumnName("Id"); this.Property(t =&gt; t.Caption).HasColumnName("Caption"); // Relationships this.HasRequired(t =&gt; t.Category) .WithMany(t =&gt; t.Products) .HasForeignKey(d =&gt; d.CategoryId) .WillCascadeOnDelete(false); } } </code></pre> <p>so when i want to delete a category that some products related to it, and DbUpdateException accurred. and in error message of exception write :</p> <pre><code>{"The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Products_dbo.Categories_CategoryId\". The conflict occurred in database \"TestDb\", table \"dbo.Products\", column 'CategoryId'.\r\nThe statement has been terminated."} </code></pre> <p>there are any error code to find out that when accurred DbUpdateException this is related to deleting dont cascade records? i know sql server return error number 547 but what about entity framework?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload