Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create entity with both primary key and identity column in EF 6

I already have a table with a varchar primary key. This is working fine with my current .edmx model.

Now I added an auto-increment identity column in that table. While I try to update the .edmx, that table is not being included in .edmx.

Can't I have a varchar PK column and an auto-increment identity column in the same table?

like image 911
Kafi Avatar asked Dec 05 '25 04:12

Kafi


1 Answers

Yes, here's an example. The identity property has nothing to do with the PK. It just won't ever be null, obviously, and increments from the seed based off what ever you set.

create table myTable ( VC varchar(64) not null
                        ,primary key (VC)
                        )

insert into myTable
values
('something')
,('else')


select * 
from myTable

alter table myTable
add id int identity (1,1)


insert into myTable (VC)
values
('thirdColumn')


select * 
from myTable
like image 60
S3S Avatar answered Dec 06 '25 22:12

S3S