c# - EF6 Code First Key not auto incrementing -
i have class model:
public class results { [key] [databasegenerated(databasegeneratedoption.identity)] public int id { get; set; } public int gameid { get; set; } public games game { get; set; } public string notes { get; set; } public virtual icollection<resultitems> resultitems { get; set; } }
for reason id field not set auto increment per schema:
create table [dbo].[results] ( [id] int not null, [gameid] int not null, [notes] nvarchar (max) null, constraint [pk_dbo.results] primary key clustered ([id] asc), constraint [fk_dbo.results_dbo.games_gameid] foreign key ([gameid]) references [dbo].[games] ([id]) on delete cascade ); go create nonclustered index [ix_gameid] on [dbo].[results]([gameid] asc);
i've added:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<results>().haskey(p => p.id).property(p => p.id).hasdatabasegeneratedoption(databasegeneratedoption.identity); base.onmodelcreating(modelbuilder); }
to db context still migrations not make field auto increment. can see i'm doing wrong?
thanks.
paul.
sql server not support alter column add identity feature. while table created , , don't want loss data , cant automaticly, table,drop column(ignore in ef), , recreate table or column ef, abd restore values
Comments
Post a Comment