Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle Primary Key in Entity Framework 5 Code First

In my EF5 code-first models, creation of new records works better if the database sets the primary key. I am using a Guid for primary key and if DatabaseGeneratedOption.Identity is set, SQL Server will always create the uniqueidentifier.

However, this causes issues when I am trying to initially seed the database. If I set the Guid in my seed method, SQL Server overrides it. If I don't set the Guid, I get a new record every time. What is a recommended solution to seed the database using pre-set Guids and keep DatabaseGeneratedOption.Identity set for my normal operations?

Example class model:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public RecordName { get; set; }
public DateTime? Created { get; set; }
public DateTime? Updated { get; set; }

Example seed method:

var record = new Record()
            {
                Id = new Guid("3B80725E-9550-4933-807F-C2FAA0942225"),
                RecordName = "New Record",
                Created = DateTime.UtcNow,
                Updated = DateTime.UtcNow,
             };
context.Record.AddOrUpdate(record);
context.SaveChanges();
like image 566
roadsunknown Avatar asked Jul 18 '13 00:07

roadsunknown


People also ask

How do I mention primary key in Entity Framework code First?

The Code First primary key convention is: Property with name " Id " or {class name} + " Id " will act as the primary key for that entity. If you will run the application, it will create _MigrationHistory and Students tables where " StudentId " is the primary key of the Students table.

How do I set primary key in Entity Framework?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

What is meant by composite primary key in Entity Framework code First?

Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys. In order to use composite keys, Entity Framework requires you to define an order for the key properties. You can do this by using the Column annotation to specify an order.

How do I use code first in Entity Framework Core?

Code-First is mainly useful in Domain Driven Design. In the Code-First approach, you focus on the domain of your application and start creating classes for your domain entity rather than design your database first and then create the classes which match your database design.


1 Answers

AddOrUpdate has an overload that allows you to specify the key

From MSDN

So you need to supply the method with the natural key:

context.Record.AddOrUpdate(c => c.RecordName, new Record()
            {
                RecordName = "New Record",
                Created = DateTime.UtcNow,
                Updated = DateTime.UtcNow,
             })
like image 85
Colin Avatar answered Oct 13 '22 11:10

Colin