Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure an Identity column using Entity Framework Core?

How do I create an Auto increment identity column in Entity Framework Core?

Obviously I can do it using fluent API for EF6 for example.

like image 822
Andrew Duffy Avatar asked Apr 15 '15 22:04

Andrew Duffy


People also ask

How do I get identity value in EF core?

EF execute each INSERT command followed by SELECT scope_identity() statement. SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. The above example will execute the following SQL in the database. WHERE @@ROWCOUNT = 1 AND [StudentID] = scope_identity();

How can we specify computed generated column in Entity Framework?

The Entity Framework Core Fluent API HasComputedColumnSql method is used to specify that the property should map to a computed column. The method takes a string indicating the expression used to generate the default value for a database column.

What is DatabaseGeneratedOption identity?

An identity column in a database (and DatabaseGeneratedOption. Identity ) denote that the column generates the value automatically upon insert. These are typically int columns, and similar, that auto-increment. An INSERT for a row that uses an identity column omit a value from the query (generated by the database)


5 Answers

In latest version of EF7 there is a new extension method to set identity column

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
  modelBuilder.Entity<MyEntity>(b =>
  {
    b.HasKey(e => e.Identifier);
    b.Property(e => e.Identifier).ValueGeneratedOnAdd();
  });
}
like image 172
vityanya Avatar answered Oct 10 '22 20:10

vityanya


Since there is very little EF7 documentation, much of what we know we have to glean from the source or unit tests. According to the following two unit tests in the EF7 source...

Here and Here

You would configure a property for Identity like this:

b.Property(e => e.Id).ForSqlServer().UseIdentity();

And you would configure a property for Sequences like this:

ForSqlServer().UseSequence();

The urls have changed due to the aspnet-core reorg, and the methods have also changed since this was first asked.

Here and Here

if (_useSequence) 
{
    b.Property(e => e.Identifier).ForSqlServerUseSequenceHiLo();
} 
else 
{
    b.Property(e => e.Identifier).UseSqlServerIdentityColumn();
}

It's possible these urls might change again (which is why I include the relevant code), but it's ridiculously easy to just look at the url and go to the site and figure out what the new url is.

Really, the whole point of my answer is that you can figure this stuff out yourself just by going and looking at the unit tests in the source code on GitHub. You shouldn't need someone to spoon feed it to you.

EDIT: Updated links to version 2.1 (still works for 1.1 and 2.0 as well)

like image 40
Erik Funkenbusch Avatar answered Oct 10 '22 22:10

Erik Funkenbusch


Looks like they changed it once again and the new method now is:

.UseIdentityColumn()
like image 23
Greg Z. Avatar answered Oct 10 '22 20:10

Greg Z.


With latest bits EF Core 1.0 and up you should use

builder.Entity<ApplicationUser>().Property<int>(nameof(ApplicationUser.AccountNo))
            .UseSqlServerIdentityColumn()
like image 30
codevision Avatar answered Oct 10 '22 22:10

codevision


Here is how to do it explicitly in the event that you want to OR it's not the default behaviour.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
  modelBuilder.Entity<MyEntity>(b =>
  {
    b.Key(e => e.Identifier);
    b.Property(e => e.Identifier).ForSqlServer().UseIdentity();
  }
}
like image 28
Andrew Duffy Avatar answered Oct 10 '22 20:10

Andrew Duffy