Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use String property as primary key in Entity Framework

I'm new in EF and try to do my first steps by code first approach in ETF6.0 and now i have a problem.

I have a property

[Key]
public string FooId { get; set; }

which is my primary key for the model.

But if I run the

PM> Update-Database

command in package manager console to update pending migrations to the database i get the following error:

Identity column 'FooId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.

If I change the PK in my model to

[Key]
public int FooId { get; set; } 

everything works fine.

But I would need the PK to be of type string because it makes absolutely sens in my case. I know there are disadvantages but for me it is necessary.

I saw an older post here How to make string as primary key in entity framework!.

But it seems not to solve my problem or I just don't understand it.

Is it really that I can't use a string as PK on a SQL database?

Or is there a way to do that?

like image 430
ck84vi Avatar asked Oct 07 '15 03:10

ck84vi


People also ask

Can string be used as primary key?

The short answer : It's perfectly fine to use a string as a primary key.

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.

Which data annotation attribute can be used to define a primary key property?

You can use the key annotation to specify which property is to be used as the EntityKey. If you are using code first's database generation feature, the Blog table will have a primary key column named PrimaryTrackingKey, which is also defined as Identity by default.

How do I create a composite primary key in Entity Framework?

Entity Framework Core supports composite keys - primary key values generated from two or more fields in the database. Composite keys are not covered by conventions or data annotation attributes. The only way to configure composite keys is to use the HasKey method.


4 Answers

This is the proper way of creating a PK without Identity Autoincrement enabled:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string FooId { get; set; }
like image 124
Nikita Shrivastava Avatar answered Sep 20 '22 19:09

Nikita Shrivastava


If you need your primary key to be a string, then don't make it an identity column. Identity columns will generate primary key values for you, which you should turn off if you intend to generate the values yourself.

like image 42
Bill Ravdin Avatar answered Sep 23 '22 19:09

Bill Ravdin


What is your reason for having a string as a primary key?

I would just set the primary key to an auto incrementing integer field, and put an index on the string field.

That way if you do searches on the table they should be relatively fast, and all of your joins and normal look ups will be unaffected in their speed.

You can also control the amount of the string field that gets indexed. In other words, you can say "only index the first 5 characters" if you think that will be enough. Or if your data can be relatively similar, you can index the whole field.

like image 2
Rakin Avatar answered Sep 23 '22 19:09

Rakin


Another possible answer for others not wanting to alter their entities is telling the DbContext:

  builder.Entity<Food>(b =>
  {
       b.Property(u => u.FooId).HasDefaultValueSql("newsequentialid()");
  });

This will tell the DbContext that the Food model has an Id named FoodId and it will requires an ID generation.

EF Core default values: MSDN

like image 2
Jose Cordero Avatar answered Sep 21 '22 19:09

Jose Cordero