1

I have an Entity Framework context that is generated Database-first. I'm adding a table with a GUID PK that will be generated by the database. When I generate the EDMX model from the database, it sets StoreGeneratedPattern to 'None'.

I need to manually change StoreGeneratedPattern to 'Identity' for the application to let the database create the ID's.

Is there a way to ensure that EDMX automatically sets this value correctly?

awright
  • 1,224
  • 1
  • 13
  • 23
  • `uniqueidentifier` columns can't have an identity specification, so you can't expect EF to know when to set it as identity. – Gert Arnold Sep 29 '17 at 14:51
  • I see that EF Core handles this better. It detects default constraints in SQL Server and creates the EF model accordingly. – awright Sep 29 '17 at 18:29

1 Answers1

5

In Entity Framework 6 it's just the way it is. When uniqueidentifier columns are primary key and have a default value, the EDMX generator tool doesn't mark them as DatabaseGeneratedOption.Identity. In a strict sense that's right, because the column doesn't have (can't have) an identity specification.

So in the EDMX designer you have to set the StoreGeneratedPattern property of the Id column(s) to Identity manually. The good new is that this survives model updates.

The net effect for EF is similar to a true (= numeric) identity column. Whether or not an explicit value is assigned to the Id column:

  • The column is ignored in INSERT statements
  • The generated value is read into the entity during SaveChanges.

As indicated by your comment, Entity Framework-core (current version 2.0.0) handles this differently. When a model is generated from a database (package manager console: Scaffold-DbContext), you'll see an entry in the context's OnModelCreating override looking like this:

entity.Property(e => e.Id)
    .HasColumnName("ID")
    .HasDefaultValueSql("(newsequentialid())");

But the behavior is subtly different than in EF6. When no value is set for the Id column (i.e. it is a default Guid) the behavior is still the same:

  • The column is ignored in the INSERT statement
  • The generated value is read into the entity during SaveChanges.

However, when the Id column is set to another value than the default:

  • The column is part of the INSERT statement
  • There is no generated value.

That means that, other than in EF6, as a developer you have to take care that no explicit values are assigned to Guid PK properties if you want them to be always generated by the database.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thanks for the in-depth answer. EF Core is the more complete solution in my mind. Will likely give that a try. – awright Sep 30 '17 at 01:11
  • Bear in mind that EF-core isn't as mature as EF6 yet, although it's [catchinp up](https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap). – Gert Arnold Sep 30 '17 at 20:47
  • Yes, thanks. Need to determine if the lack of maturity is acceptable for our application. – awright Oct 02 '17 at 14:49