Kevin Krumwiede
Kevin Krumwiede

Reputation: 10308

Can I insert ID 0 in MySQL with EF Core HasData?

I'm writing a server for a client that I do not control. I'm using HasData to seed the database with static information that the client refers to by ID. The client uses 1-based numbering for almost everything, but there is one entity that uses 0-based numbering.

My working solution is to add a ClientId column to the table. But I wonder if there's a way to configure NO_AUTO_VALUE_ON_ZERO or something, using EF attributes or fluent config, and only for that one table, so that HasData can insert 0.

Upvotes: 0

Views: 39

Answers (1)

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10308

The solution was obvious in retrospect. An ID value of 0 is only disallowed because it normally represents auto-assign. I simply added [DatabaseGenerated(DatabaseGeneratedOption.None)] to the entity's ID property. As I mentioned, this data is static so there won't be any inserts to worry about.

Upvotes: 1

Related Questions