Set identity to the previous created column during migration

ef-code-first ef-migrations entity-framework entity-framework-6

Question

I'm working on a project that includes two migration processes using the CodeFirst database (Entity Framework 6). This code is used in Application Start in Global.asax to automatically update the database:

Database.SetInitializer(
          new MigrateDatabaseToLatestVersion<MyDBEntities, MyNamespace.Configuration>());

Table creation is the first migration step:

CreateTable(
     "dbo.GalleryAlbum",
      c => new
      {
           Id = c.Int(nullable: false),
           //other columns.....
       })
       .PrimaryKey(t => t.Id);

CreateTable(
       "dbo.GalleryPics",
       c => new
       {
           Id = c.Int(nullable: false),
           //other columns.....
       })
       .PrimaryKey(t => t.Id)
       .ForeignKey("dbo.GalleryAlbum", t => t.AlbumId)
       .Index(t => t.AlbumId);

Adding identities to newly generated tables is the second migration step:

AlterColumn("dbo.GalleryAlbum", "Id", c => c.Int(nullable: false, identity: true));
AlterColumn("dbo.GalleryPics", "Id", c => c.Int(nullable: false, identity: true));

When I launch the application, I can see that the second migration function is executing and adding information about two migrations to the _MigrationHistory table, but neither table's columns have changed (without Identity). The plan is as follows:

[Id]        INT             NOT NULL,
//other columns

Code The following are the first classes for the initial migration:

 public partial class GalleryAlbum
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
 }
 //GalleryPics is the same

and this one for the next stage of migration:

public partial class GalleryAlbum
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
 }
 //GalleryPics is the same

Please let me know the reason Identity isn't included in these columns and how I can solve it.

Thanks.

Update: I sent database update requests, which I received from the IDbCommandInterceptor:

ALTER TABLE [dbo].[GalleryAlbum] ALTER COLUMN [Id] [int] NOT NULL
ALTER TABLE [dbo].[GalleryPics] ALTER COLUMN [Id] [int] NOT NULL
1
2
2/16/2014 1:16:20 PM

Accepted Answer

ZZZ_tmp
2
5/23/2017 11:53:38 AM

Popular Answer

According to the acceptable response, the problem can be resolved if you simply include the identification while establishing the table in the first place. However, if the table already exists and you have access to SSMS, I think the simplest method is to:

  1. Make a database backup.
  2. Modify your class so that it has a name in your code.
  3. Use the Package Manager Console to create the migration (add-migration [your migration name] )
  4. Remove the code's commentsUp a technique used in the newly created migration
  5. Create a new line of code that is prepared to accept the SQL you will produce below:Sql (@" ");
  6. Verify that SSMS is configured to create scripts whenever you make a table modification by going there.
  7. Include the identity in the SMSS table designer.
  8. Copy the produced SQL after saving the table modification in SSMS. (SQL replicates the table containing the data, deletes the original table, recreates the original table with the identity set on the column, copies the original table's contents back, and then inserts all the constraints and foreign keys once more.)
  9. In the code you added earlier, between the speech marks, paste the SQL you just copied.
  10. Activate the migration

You should then receive a migration as a result of that, which adds the identity and may be successfully executed on additional copies of the database.

NB:The Down The produced method will also fail since it will remove the identity, something EF cannot do as well. If you require theDown approach to work To build the table without the identity once more, make a copy of the SQL you added and make the necessary changes.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow