-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Concatenated string sequence as primary key - Insertion fail via EF code #8239
Comments
I have called 'HasDefaultValueSql'. That's how I set the default value, which works on manual insert |
@urielb92 Can you post some code that reproduces the issue? A full project or code listing is preferable, but just your entity types and OnModelCreating may be sufficient. |
Sure, I'll post the the relevant parts later when I have access to the code |
@ajcvickers Sorry for the delay, posting the relevant parts of the code. The entity : public partial class TheEntity
{
[Column("EntityColumnNameInDb")]
public string Id { get; set; }
// Some other properties
} DbContext : protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// ...
modelBuilder.Entity<TheEntity>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PK_EntityTable");
entity.Property(e => e.Id).ValueGeneratedNever();
// ...
}
// ...
} The migration : protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"CREATE SEQUENCE EntitySequence START WITH 1 INCREMENT BY 1;");
migrationBuilder.AlterColumn<string>(
name: "EntityColumnNameInDb",
table: "EntityTable",
defaultValueSql: "'i' + CAST((NEXT VALUE FOR EntitySequence) AS VARCHAR(20))",
nullable: false,
oldClrType: typeof(int),
oldDefaultValueSql: null);
} I think that's all the relevant parts. Thanks for the help. |
@urielb92 This line tells EF that you never need any kind of store-generated value: entity.Property(e => e.Id).ValueGeneratedNever(); so you need to remove that, Then, as explained in #4189 and #7872, you need to put this in your OnModelCreating: entity.Property(b => b.Id)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("'i' + CAST((NEXT VALUE FOR EntitySequence) AS VARCHAR(20))"); This tells EF to generate values and that the column has SQL to do this. |
@ajcvickers Hi, I've tried your suggestion before (as described in the main post), and it resulted with the Id being generated as a GUID, totally ignoring the default value. Just to make sure, tried it again - same results. |
…olumn has default SQL Otherwise we just send the generated key to the database. Issue #8239
…olumn has default SQL Otherwise we just send the generated key to the database. Issue #8239
…olumn has default SQL Otherwise we just send the generated key to the database. Issue #8239
Describe what is not working as expected:
I want to use a concatenated string to a sequence as a primary key of a table.
I have created the table using Code-First migration successfully, and have set the default value of the column to be the desired concat.
When I insert rows to the table manually using the INSERT command, it works as expected. If I choose to omit the value of the column, it is generated from the sequence, otherwise - it uses the value provided.
However, when I try to insert entities by code - an exception is being thrown.
The relevant field of the entity is a simple string property. I tried adding
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
(also identity, and none) annotation, but that didn't do the trick. When I set the property to Identity (I called directly theValueGeneratedOnAdd
through the fluent api) it just generated a GUID, not taking into consideration the default value provided.If you are seeing an exception, include the full exceptions details (message and stack trace).
I can think of workarounds such as first generating the value manually and only then updating the entity and saving it, or having a column for the sequence generated value, and a trigger update that column, but I'd like to avoid them if possible.
Further technical details
EF Core version: (found in project.json or packages.config) - 1.1.1
Database Provider - Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows
IDE: Visual Studio 2017
(Also opened on StackOverflow)
The text was updated successfully, but these errors were encountered: