additional migration tries to redefine existing identity columns resulting in incorrect T-SQL syntax
ef : 7.0.0-beta5-13012 target : net45
Adding/applying another migration after initial tries to alter tables like this: ALTER TABLE [Animal] ALTER COLUMN [Id] int NOT NULL IDENTITY; ALTER TABLE [Zoo] ALTER COLUMN [Id] int NOT NULL IDENTITY; ALTER TABLE [ZooAnimalSighting] ALTER COLUMN [Id] int NOT NULL IDENTITY;
this is incorrect syntax and these columns are already defined as Identity
here is the initial migration:
public override void Up(MigrationBuilder migration)
{
migration.CreateSequence(
name: "DefaultSequence",
type: "bigint",
startWith: 1L,
incrementBy: 10);
migration.CreateTable(
name: "Zoo",
columns: table => new
{
Id = table.Column(type: "int", nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity"),
City = table.Column(type: "nvarchar(max)", nullable: true),
Name = table.Column(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Zoo", x => x.Id);
});
migration.CreateTable(
name: "Animal",
columns: table => new
{
Id = table.Column(type: "int", nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity"),
Group = table.Column(type: "int", nullable: false),
Name = table.Column(type: "nvarchar(max)", nullable: true),
Weight = table.Column(type: "real", nullable: false),
ZooId = table.Column(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Animal", x => x.Id);
table.ForeignKey(
name: "FK_Animal_Zoo_ZooId",
columns: x => x.ZooId,
referencedTable: "Zoo",
referencedColumn: "Id");
});
migration.CreateTable(
name: "ZooAnimalSighting",
columns: table => new
{
Id = table.Column(type: "int", nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity"),
AnimalId = table.Column(type: "int", nullable: true),
Comment = table.Column(type: "nvarchar(max)", nullable: true),
DoYouWearGlasses = table.Column(type: "bit", nullable: true),
Sighter = table.Column(type: "nvarchar(max)", nullable: true),
SightingDate = table.Column(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ZooAnimalSighting", x => x.Id);
table.ForeignKey(
name: "FK_ZooAnimalSighting_Animal_AnimalId",
columns: x => x.AnimalId,
referencedTable: "Animal",
referencedColumn: "Id");
});
}
T-SQL for create tables is:
CREATE TABLE [Zoo] (
[Id] int NOT NULL IDENTITY,
[City] nvarchar(max),
[Name] nvarchar(max),
CONSTRAINT [PK_Zoo] PRIMARY KEY ([Id])
);
CREATE TABLE [Animal] (
[Id] int NOT NULL IDENTITY,
[Group] int NOT NULL,
[Name] nvarchar(max),
[Weight] real NOT NULL,
[ZooId] int,
CONSTRAINT [PK_Animal] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Animal_Zoo_ZooId] FOREIGN KEY ([ZooId]) REFERENCES [Zoo] ([Id])
);
CREATE TABLE [ZooAnimalSighting] (
[Id] int NOT NULL IDENTITY,
[AnimalId] int,
[Comment] nvarchar(max),
[DoYouWearGlasses] bit,
[Sighter] nvarchar(max),
[SightingDate] datetime2 NOT NULL,
CONSTRAINT [PK_ZooAnimalSighting] PRIMARY KEY ([Id]),
CONSTRAINT [FK_ZooAnimalSighting_Animal_AnimalId] FOREIGN KEY ([AnimalId]) REFERENCES [Animal] ([Id])
);
then subsequent migration
public override void Up(MigrationBuilder migration)
{
migration.DropColumn(name: "Comment", table: "ZooAnimalSighting");
migration.DropColumn(name: "DoYouWearGlasses", table: "ZooAnimalSighting");
migration.AlterColumn(
name: "Id",
table: "Animal",
type: "int",
nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity");
migration.AlterColumn(
name: "Id",
table: "Zoo",
type: "int",
nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity");
migration.AlterColumn(
name: "Id",
table: "ZooAnimalSighting",
type: "int",
nullable: false)
.Annotation("SqlServer:ValueGeneration", "Identity");
migration.AddColumn(
name: "CanYouSeeOk",
table: "ZooAnimalSighting",
type: "bit",
nullable: true);
}
T-SQL is:
ALTER TABLE [ZooAnimalSighting] DROP COLUMN [Comment];
ALTER TABLE [ZooAnimalSighting] DROP COLUMN [DoYouWearGlasses];
ALTER TABLE [Animal] ALTER COLUMN [Id] int NOT NULL IDENTITY;
ALTER TABLE [Zoo] ALTER COLUMN [Id] int NOT NULL IDENTITY;
ALTER TABLE [ZooAnimalSighting] ALTER COLUMN [Id] int NOT NULL IDENTITY;
ALTER TABLE [ZooAnimalSighting] ADD [CanYouSeeOk] bit;
INSERT INTO [dbo].[__MigrationHistory] ([MigrationId], [ContextKey], [ProductVersion])
VALUES ('20150425173114_ModifyZooAnimalSighting', 'SightingsWebAppDemo.DAL.ZooSightingDemoDb', '7.0.0-beta5-13012');
;
Data model is:
public class ZooAnimalSighting
{
public int Id { get; set; }
public string Sighter { get; set; }
public DateTime SightingDate { get; set; }
public bool? CanYouSeeOk { get; set; }
// Navigation
public virtual Animal Animal { get; set; }
}
public class Zoo
{
public Zoo()
{
Animals = new HashSet();
}
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
// Navigation
public virtual ICollection<Animal> Animals { get; set; }
}
public class Animal
{
public int Id { get; set; }
public string Name { get; set; }
public float Weight { get; set; }
public AnimalGroup Group { get; set; }
// Navigation
public virtual Zoo Zoo { get; set; }
}
public enum AnimalGroup
{
Reptile = 1,
Fish,
Invertebrate,
Amphibian,
Mammal,
Bird
}
A more root cause may be that this T-SQL is sent to database on each subsequent migration, this is without identity specified in model. This is valid T-SQL so does not generate error. but in the previous identity case it is was not valid T-SQL.
ALTER TABLE [Animal] ALTER COLUMN [Id] int NOT NULL; ALTER TABLE [Zoo] ALTER COLUMN [Id] int NOT NULL; ALTER TABLE [ZooAnimalSighting] ALTER COLUMN [Id] int NOT NULL;
It looks like we ignored IDENTITY during alter column operations in EF6. For now, we'll do the same on EF7. With #329 and #1911, we could do a bit more.