Note that there are some explanatory texts on larger screens.

plurals
  1. POCode first model not auto incrementing key on insert
    primarykey
    data
    text
    <p>I think I have an error in my model, but I'm not sure what it is.</p> <p>First the error</p> <pre><code>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.ProjectDocAccess_dbo.ProjectDoc_ProjectDocAccessID". The conflict occurred in database "dbname", table "dbo.ProjectDoc", column 'ProjectDocID'. </code></pre> <p>The <code>ProjectDocAccess</code> model (trimmed down)</p> <pre><code>public class ProjectDocAccess { public int ProjectDocAccessID { get; set; } public int ProjectDocID { get; set; } public virtual ProjectDoc ProjectDoc { get; set; } } </code></pre> <p>The <code>ProjectDoc</code> model (trimmed down)</p> <pre><code>public class ProjectDoc { public int ProjectDocID { get; set; } public int ProjectID { get; set; } public ProjectDocAccess ProjectDocAccess { get; set; } public Project Project { get; set; } } </code></pre> <p>The fluent API mapping</p> <pre><code> modelBuilder.Entity&lt;ProjectDocAccess&gt;() .HasRequired(p =&gt; p.ProjectDoc).WithOptional() .WillCascadeOnDelete(true); </code></pre> <p>When I attempt to insert a new record in the <code>ProjectDocAccess</code> table, it forces me to insert a value in the <code>ProjectDocAccessID</code> field. In all of my other models, this auto increments. I'm not sure what I am doing wrong. Help?</p> <p><strong>UPDATE</strong></p> <p>Based on the answer I selected. This is what I did to fix it.</p> <p>Removed the fluent API mapping altogether.</p> <p>Updated the <code>ProjectDocAccess</code> model as follows</p> <pre><code> public class ProjectDocAccess { [Key, ForeignKey("ProjectDoc")] public int ProjectDocAccessID { get; set; } public virtual ProjectDoc ProjectDoc { get; set; } } </code></pre>
    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.
    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