httpflow icon indicating copy to clipboard operation
httpflow copied to clipboard

Seeding failing when creating new product

Open whyzlo opened this issue 1 month ago • 1 comments

https://github.com/ardalis/CleanArchitecture/blob/5770078450386b47be40f31ca10ee9cb8dd61353/MinimalClean/src/MinimalClean.Architecture.Web/Domain/ProductAggregate/Product.cs#L12

This logic needs to be inverted to id != ProductId.New as predicate is checked inside InvalidInput this way if (!predicate(input)). Because of this seeding is failing when running minimal version for this template and seeding database:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentException: Use Product.Create() to create new products instead of passing ProductId.New to the constructor. (Parameter 'id')

Also there is a problem with Product creation, as it is using this: return new Product(ProductId.New, name, unitPrice);

So after the changes it won't be able to create new ones

whyzlo avatar Dec 07 '25 08:12 whyzlo

Good catch, thanks. I should have tests for these. Also typically I would make the constructor private to force the use of Create; trying to recall why I didn't do so in this case.

I've been running into issues in a project similar to this one with Postgres and int identity columns. The use of Vogen was causing problems with the EF tracking as EF only wanted to generate the id for the identity columns if it was not set, but Vogen doesn't let you "not set" an instance. In that project I ended up just switching to guid keys because they worked better anyway but I need to resolve the general problem for identity columns.

ardalis avatar Dec 08 '25 10:12 ardalis