DatabaseGenerated(DatabaseGeneratedOption.Identity) vs Key

Viewed 6857

Looking at code first, I see some examples that use [DatabaseGenerated(DatabaseGeneratedOption.Identity)] to denote a primary key, and other examples that use [Key].

I haven't been able to find a description of how the two compare.

Can someone tell me when/if I'd want to use one over the other?

3 Answers

One specifies an explicit Primary Key , the other specifies that it should be an identity field in that database (which depends on the provider). In a lot of cases, the Key will be an identity field.

DatabaseGeneratedOption.Identity

You can mark the non-key (non-id) properties as DB-generated properties by using the DatabaseGeneratedOption.Identity option. This specifies that the value of the property will be generated by the database on the INSERT statement. This Identity property cannot be updated.

Please note that the way the value of the Identity property will be generated by the database depends on the database provider. It can be identity, rowversion or GUID. SQL Server makes an identity column for an integer property.

The Key Attribute

The Key attribute can be applied to a property in an entity class to make it a key property and the corresponding column to a PrimaryKey column in the database. The default convention creates a primary key column for a property whose name is Id or Id. The Key attribute overrides this default convention.

if you wanted to specify a non identity primary key, you may use something like this

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

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)

On the other hand, just because a column is a primary key does not mean it's an identity column. There are many cases where you don't just want the database to automatically generate a value for a column. And data types that can't be identity columns.

  • An INSERT for a row that's a PK but not an identity column almost always specifies the value in the query (not generated by the database).

In short you can think of an identity column as something like a GUID. It uniquely identifies a row but is not useful data in and of itself.

Contrast that with a primary key column "email_address" and it certainly wouldn't be an identity column. Plus it's legitimate data.

DatabaseGeneratedOption.Identity:

Eg:

public class Car

{

    [key]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]

    public int Id { get; set; }

    public string Model { get; set; }

    public DateTime Registered { get; set; }

}

Let's say I have a class name Car, I have different properties lets say Id, Model and Registered in the class, When you are doing operation like inserting data in database, you do not have to specify the ID column value(like 1, 2, 100, 141), database will take care of ID column for you. If you set the auto increment to 1, your first record will start with 1 and it auto increments like 2,3,4... and the key attribute at the top means that it is primary key.

Related