I am doing an ASP.NetCore MVC Project.
Following is the code I am using to insert the primary key of the table 'ImageFile'
.
(It has attributes like: Id
, UserID
, ImageName
, Size
, URl
etc. UserId is a foreign key).
ImageFile imageFile = new ImageFile();
imageFile.Id = new Random().Next();
This just inserts a random number into the Id
field of the table.
Ex:
ID | UserId | ImageName |
---|---|---|
1513570791 | 2 | image1.png |
1714580687 | 2 | image2.png |
5909420604 | 2 | image3.png |
How can I modify the above code snippet to insert an auto-incrementing value to the table ?
Ex:
ID | UserId | ImageName |
---|---|---|
1 | 2 | image1.png |
2 | 2 | image2.png |
3 | 2 | image3.png |
Thank you.
ImageFile imageFile = new ImageFile(); imageFile.Id = new ValueGeneratedOnAdd();