Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary key types #1046

Closed
samuela opened this issue Oct 9, 2018 — with docs.microsoft.com · 10 comments · Fixed by #1907
Closed

Primary key types #1046

samuela opened this issue Oct 9, 2018 — with docs.microsoft.com · 10 comments · Fixed by #1907

Comments

Copy link

samuela commented Oct 9, 2018

It would be nice to have a discussion of the recommended type choice(s) for primary keys. The docs here use string but what about int, uint, long, and so on? Are there types that are not allowed as primary keys?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Copy link

What about composite keys? Seems like a basic use case that should be covered here. Specifically, are there annotations for that?

Copy link

kkarakk commented Dec 4, 2018

https://medium.com/radzenhq/how-to-entity-framework-core-relationships-composite-keys-foreign-keys-data-annotations-code-f4b238086463
for anyone searching for composite keys, you first define two or more of the columns required as the composite key as [Key] in the class and then specify them in model by overriding the following method in DBContext

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity().HasKey(table => new {
table.OrderID, table.ProductID
});
}

@ajcvickers
Copy link
Contributor

@kkarakk Using the [Key] attribute in that case should not be needed.

Copy link

eeegs commented Dec 17, 2018

I'm surprised at the docs '...conventions will never setup a composite key...', EF6 did it why can't core. Surely the principles are the same. Using composite keys is very common in a multitenancy environment.

@ajcvickers
Copy link
Contributor

@eeegs Can you remind me in what conditions EF6 creates a composite key by convention?

@eeegs
Copy link

eeegs commented Dec 18, 2018

@ajcvickers: My bad, I was thinking attributes when I read that in the doco. Because at the moment even multiple '[Key, Column(order=n)]' can't be used. Sorry never by convention will always be correct.

Copy link
Contributor

I would say GUID would be a good fit for an ID, if you plan to work with a large amount of records

Copy link

Guids for PK are only acceptable if they are mostly sequentially generated.

Copy link

Related, it would be good to at least link to the Generated Values page because there's valuable information regarding keys there: "By convention, non-composite primary keys of type short, int, long, or Guid will be setup to have values generated on add. All other properties will be setup with no value generation."

@divega divega assigned AndriySvyryd and unassigned divega Sep 18, 2019
@ajcvickers
Copy link
Contributor

From dotnet/efcore#18055 ensure that it is clear when a key property gets generated values by default, and when not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants