Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

docs: EFCore IDs don't have to be classes #876

@JoschiZ

Description

@JoschiZ

The docs say that ids need to be classes for EFCore to work correctly.

For the Id field, because it's being used as a primary key, it needs to be a class because EFCore compares it to null to determine if it should be auto-generated. When it is null, EFCore will use the specified SomeIdValueGenerator, which looks like:

This is not entirely correct as EFCore IDs can also be arbitrary structs.
The value of the id property is compared against the sentinel value to determine if it needs to be generated / is set or not.
This is the default value of the struct unless explicitly configured to be something else using HasSentinel.

A working configuration would be:

[ValueObject<int>(Conversions.EfCoreValueConverter)]
public readonly partial struct MyId;

public class MyEntity
{
    public MyId Id { get; private init; }  // efcore needs a setter but we typically don't want to expose it
}

internal sealed class MyEntityConfig : IEntityTypeConfiguration<MyEntity>
{
    public void Configure(EntityTypeBuilder<MyEntity> builder)
    {
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id)
            .HasVogenConversion()
            .ValueGeneratedOnAdd();
    }
}

If the object uses any kind of validation deserializationStrictness: DeserializationStrictness.AllowAnything has to be added or int.MinValue has to pass the validation see #770.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions