Replies: 6 comments 3 replies
-
Also I tried updating the base class to explicitly set the Id to |
Beta Was this translation helpful? Give feedback.
-
Glad to hear about my other question - any thoughts on this one? I'd prefer to avoid having to deal with nullable Ids everywhere, and it's only ever an issue on initial creation. |
Beta Was this translation helpful? Give feedback.
-
Hi, The way I got this working was: [assembly: VogenDefaults(
staticAbstractsGeneration: StaticAbstractsGeneration.MostCommon | StaticAbstractsGeneration.InstanceMethodsAndProperties,
conversions: Conversions.EfCoreValueConverter)] This tells Vogen to generate static abstract interfaces and, along with this, generate the interface declarations for the public partial struct BlogId : ...IVogen<BlogId, int> Given that, we can put some generic constraints on a value generator: internal class VogenIdValueGenerator<TContext, TEntityBase, TId> : ValueGenerator<TId>
where TContext : DbContext
where TEntityBase : EntityBase<TEntityBase, TId>
where TId : IVogen<TId, int> However, I did have to change
We can the do: modelBuilder.Entity<Blog>(b =>
{
b.HasKey(x => x.Id);
b.Property(e => e.Id)
.HasValueGenerator<VogenIdValueGenerator<BloggingContext, Blog, BlogId>>()
.HasVogenConversion();
}); There may be other ways of doing this, but I hope this helps. Please do let me know how you get on if there's anything else I can help with. |
Beta Was this translation helpful? Give feedback.
-
Hi - I forked and created a PR for your branch. I think it does everything you want. Please do let me know if it doesn't or if you have any ideas on how to make it more 'elegant' (I don't think it'll ever be elegant, but it could probably cause less friction!) Also, re. the |
Beta Was this translation helpful? Give feedback.
-
I updated the public Ardalis.SharedKernel so it includes the base type needed. I added ProjectId and ToDoItemId value objects and configured both of them to use VogenIdValueGenerator<T,T,T> but I still see the same error in my tests. I've pushed my latest changes to my branch here:
|
Beta Was this translation helpful? Give feedback.
-
Hi Steve, you were missing the This worked when creating projects, but failed when adding a todo item. This was because the specifications used the primitive type in the where clause. Changing this to I'll update my PR with the changes |
Beta Was this translation helpful? Give feedback.
-
Hey Steve,
I'm trying to use Vogen for strongly typed IDs in my projects, many of which leverage something like Ardalis.SharedKernel:
https://github.com/ardalis/ardalis.sharedkernel
Specifically there's base entity class that a lot of other types like repositories and specifications depend on, which looks like this:
Ok so if I just use this as-is, I get this exception:
It doesn't like that I never set the value. I looked at your docs and it seems to suggest I use a nullable type for this scenario, but that would break a lot of things. I could specify something like an instance value:
But I don't see how to use this generically from BaseEntity and if I override the Id property using
new
then other things break.Any thoughts on how to support Vogen Id types in a generic way following the convention EF Core uses that value types that match their defaults represent net new entities and thus should be treated as New by the change tracker?
Thanks,
Ardalis
Beta Was this translation helpful? Give feedback.
All reactions