Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot insert explicit value for identity column
    primarykey
    data
    text
    <p>I'm getting the above error when creating a new record. I don't want to insert the identity -- the database has it auto generated and that is great.</p> <p>Here is the exact exception:</p> <pre><code>[System.Data.UpdateException] {"An error occurred while updating the entries. See the inner exception for details."} {"Cannot insert explicit value for identity column in table 'PartRevisions' when IDENTITY_INSERT is set to OFF."} </code></pre> <p>Here are the mappings:</p> <pre><code>Public Class PartContext Inherits DbContext Public Property Parts As DbSet(Of Part) Public Property PartRevisions As DbSet(Of PartRevision) Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder) MyBase.OnModelCreating(modelBuilder) modelBuilder.Entity(Of PartRevision)().HasKey(Function(r) r.Id).Property(Function(r) r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) End Sub End Class &lt;Table("Parts")&gt; Public Class Part &lt;Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)&gt; Public Property Id As Integer Public Property PartNumber As String Public Property Owner As String End Class &lt;Table("PartRevisions")&gt; Public Class PartRevision Inherits Part Public Property OriginalId As Integer Public Property RevisionDate As DateTime Public Property RevisionNumber As Integer Public Property RevisionBy As String End Class </code></pre> <p>If I don't use inheritance it works fine. If I make Id overridable and also specify the attributes on the sub class it still doesn't work.</p> <p>I'm doing the <code>OnModelCreating</code> stuff just because I'm trying to get it to work. I feel like it should work without this. Of course it doesn't even work with this...</p> <p>The following query works fine when I execute it in SQL Management Studio:</p> <pre><code>insert into PartRevisions (originalid, revisiondate, revisionnumber, revisionby, partnumber, owner) values (1, '1/1/2013', 1, 'eep', '123', 'ME') </code></pre> <p>Here is a gist with the full program. I was trying this out in a test project because I assumed I'd run into some issues doing inheritance in EF (never done yet).</p> <p><a href="https://gist.github.com/eyston/4956444" rel="nofollow">https://gist.github.com/eyston/4956444</a></p> <p>Thanks!</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