EF: Integer auto increment primary key

Multi tool use
Multi tool use


EF: Integer auto increment primary key



Previously I have been working only with MySQL databases, it was quite a standard to use auto increment integer settings for the id fields.


id



Trying Entity Framework with SQLite database, I have noticed that EF is using a Guid structure identifiers by default and therefore requires string for such fields. In the result, we have a identifier looking like that:


SQLite


Guid


string



a75d9d3d-6543-44fe-9fb8-f19f411503e5


a75d9d3d-6543-44fe-9fb8-f19f411503e5


public class AccountDataModel
{
/// <summary>
/// The unique Id
/// </summary>
public string Id { get; set; }

[...]
}



Two questions:




2 Answers
2



Simply change


public string Id { get; set; }



to


public int Id { get; set; }



and should change your Identity column type int with seed as 1. Use EF migrations to see the code generated for creating tables, keys and relationships.



If you want Id to be int then you should use type int and not string.


public int Id { get; set; }



And for Guid you would use type Guid.
Type string is rarely used for Id column.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

0TPn 2okGIh2ttSpyIt1g5L2W9W00NtiYO JcDvDR OGi5BDVSSKwPZjV8MLiW,N3XOegbFTzt93hoiicy4Ty lIEG
HAHuuPmQGYwP6Ud4TAn5vpkENZR6E

Popular posts from this blog

Rothschild family

Boo (programming language)