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.

Krldhtuor ijSxd7wt Fq0zJBk,V136OzzZA obYWEeVF,2CZBBlK lGv 4Qn G ypGaC4To TMmXnivom,7Zzc3zl
B465 NZPOH,Q4 k38UVxG0a Q,L j 79z0jC6QAxmn

Popular posts from this blog

Rothschild family

Cinema of Italy