Note that there are some explanatory texts on larger screens.

plurals
  1. POadding a foreign key with Code First migration
    text
    copied!<p>I have an error when trying to update my database after adding a migration.</p> <p>Here are my classes before add-migration</p> <pre><code>public class Product { public Product() { } public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } public bool Istaxable { get; set; } public string DefaultImage { get; set; } public IList&lt;Feature&gt; Features { get; set; } public IList&lt;Descriptor&gt; Descriptors { get; set; } public IList&lt;Image&gt; Images { get; set; } public IList&lt;Category&gt; Categories { get; set; } } public class Feature { public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } } </code></pre> <p>Now I wanted to add a foreign key in my Feature class and refactored the classes this way:</p> <pre><code>public class Product { public Product() { } public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } public bool Istaxable { get; set; } public string DefaultImage { get; set; } public IList&lt;Feature&gt; Features { get; set; } public IList&lt;Descriptor&gt; Descriptors { get; set; } public IList&lt;Image&gt; Images { get; set; } public IList&lt;Category&gt; Categories { get; set; } } public class Feature { public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } public string Image { get; set; } public string VideoLink { get; set; } public int ProductId { get; set; } public Product Product { get; set; } } </code></pre> <p>I added a migration with <code>Add-Migration</code> command. I added an <code>Update-Database</code> command and here is what I got back:</p> <blockquote> <p>The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.ProductFeatures_dbo.Products_ProductId". The conflict occurred in database "CBL", table "dbo.Products", column 'ProductId'</p> </blockquote> <p>What can I do to solve this problem and get my migrations back to normal?</p>
 

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