Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By default Entity Framework assumes that an integer primary key is database generated (equivalent to adding the attribute <code>HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)</code> or calling <code>Property(e =&gt; e.EventID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);</code> in the Fluent API. </p> <p>If you look at the migration that creates the table you should see this:</p> <pre><code> CreateTable( "dbo.Events", c =&gt; new { EventID = c.Int(nullable: false, identity: true), //etc }) .PrimaryKey(t =&gt; t.EventID ); </code></pre> <p>Then you changed the model using the Fluent API to <code>DatabaseGenerated.None</code>. EF puts this in the migration:</p> <pre><code>AlterColumn("dbo.Events", "EventID", c =&gt; c.Int(nullable: false, identity: false)) </code></pre> <p>And the sql generated is this:</p> <p><code>ALTER TABLE [dbo].[Events] ALTER COLUMN [EventID] [int] NOT NULL</code> </p> <p>Which actually does diddly squat. <a href="https://stackoverflow.com/questions/8230257/sql-server-remove-identity-from-a-column-in-a-table">Dropping the IDENTITY</a> from a column is not trivial. You need to drop and recreate the table or create a new column, then you have to copy the data and fix up foreign keys. So it's not surprising that EF isn't doing that for you. </p> <p>You need to work out how best to do it for yourself. You could roll back your migrations to 0 and re-scaffold from scratch now that you have specified <code>DatabaseGeneratedOption.None</code>, or you could change the migration manually to drop and recreate the table.</p> <p>Or you could drop and recreate the column:</p> <pre><code>DropColumn("Customer", "CustomerId"); AddColumn("Customer", "CustomerId", c =&gt; c.Long(nullable: false, identity: false)); </code></pre> <p><strong>EDIT</strong> Or you could <a href="http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/" rel="noreferrer">Switch Identity On/Off With A Custom Migration Operation</a></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