EF Core Database Generated Primary Key Support
I have the following value object:
[ValueObject<int>]
public partial class OrganizationId
{
private static Validation Validate(in int organizationId)
{
if (organizationId is <= 0)
{
return Validation.Invalid($"Organization id must be greater than 0. Value: {organizationId}");
}
return Validation.Ok;
}
}
My domain model has defined its Id property as such:
public OrganizationId Id { get; private set; } = null!;
I have Vogen generating EF Core converter:
[EfCoreConverter<OrganizationId>]
internal partial class VogenEfCoreConverters;
I have set the property in my EF configuration file:
_ = builder
.Property(entity => entity.Id)
.IsRequired()
.ValueGeneratedOnAdd()
.HasVogenConversion();
When I call SaveChanges and I have new entities that I am trying to add to the database I get:
Vogen.ValueObjectValidationException: Organization id must be greater than 0. Value: -2147482647
I see the Validate method is getting fired and is being passed the value of -2147482647 when it is trying to prepare things to perform the insert.
The first way I was able to get around this was changing the validation to say the value of -2147482647 was a valid value. I didn't think this was the right approach and wasn't sure if this value was guaranteed to be the same.
The second way I was able to get around this was adding the following:
[assembly: VogenDefaults(deserializationStrictness: DeserializationStrictness.AllowAnything)]
I prefer this approach but I was wondering if there is a better approach or a way that you can support this natively?
Thanks for the feedback @KenBrannigan . Does anything in this page help? https://stevedunn.github.io/Vogen/efcoreintegrationhowto.html
I'm no EF Core expert though. I'm pretty sure this scenario is covered. Please post back if you don't have any luck and I'll look into it.
There are 2 problems.
- Generated EF Value converter doesn't use constructor for object generation. I have created my own converter to bypass the problem.
- Look at #724 issue. There is a description on type compare behaviour.
Regarding Vogen's EF converter, I want also understand why converter doesn't use constructor. In my case we have 2 business requirements:
- Length of property for all new strings must be 1000 and lower (a new business requirement)
- Length of property in database can be 1000+ And also we have validator in own primitive type with check property.Length < 1000. Vogen's EF converter take object from database and throw an exception. It's very strange and unagile behaviour.
When the object is in the database, the error should not be generated when reading it.
Thanks for the feedback @KenBrannigan . Does anything in this page help? https://stevedunn.github.io/Vogen/efcoreintegrationhowto.html
I'm no EF Core expert though. I'm pretty sure this scenario is covered. Please post back if you don't have any luck and I'll into it.
Unfortunately no. That link mentions using HasValueGenerator, but that is not an option as I need the database to generate the id for me.