EF: Integer auto increment primary key

c# entity-framework entity-framework-core

Question

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

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:

a75d9d3d-6543-44fe-9fb8-f19f411503e5

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

    [...]
}

Two questions:

  • Why is that? Is there any particular reason NOT to use the integer identifiers? Is one better than the other?
  • Does EF allow to use integer idenfitiers? If so, how to enable that?
1
-3
7/1/2018 6:14:46 PM

Accepted Answer

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.

2
7/1/2018 6:27:46 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow