-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Is there an option to extend the model without inheriting from DbContext? #32462
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Your nuget package can integrate into the model-building process by introducing its own conventions; as an example, see EFCore.CheckConstraints which adds various check constraints to the model. To help out more, we'd need to know exactly what kind of model changes your library intends to do etc. |
Very often, I need to extend entities with service properties like Timestamp or Version and manage such properties automatically. For example, |
Or I just need to add some table for library-specific data. Like Outbox for published messages. |
Then yeah, plugin conventions seem like the thing to do - take a look at EFCore.CheckConstraints. |
Ok, thanks, I'll look at it. And if I want to intercept expression tree manipulation? As I said before, I'd like to express domain behavior in methods which work exclusively with entity's state. EF cannot translate these methods to SQL, but if method call could be replaced with its body, EF will do fine. So I want an extension coupled with source generator.
If I call
EF will understandably throw. So, my idea is to have a source generator which checks for instance method calls and creates expression equivalents:
The second step would be to replace It can be done without source generator if explicit mapping between the method and expression is provided. |
A plugin can also add method and member translators - you can look at the NodaTime and/or NetTopologySuite plugins in this repo for inspiration. I'll go ahead and close this now as the questions have been answered, but feel free to post back here if you need to. |
@roji Q1: Do I get it right that annotations is the way to extend configuration of entites? For example, I want to mark a property as a timestamp.
|
@voroninp in general yes, but I'd need to know more about exactly what you're trying to achieve. |
|
Sounds like you want to set a value generator that works on update (not yet supported, #6999). Until then, although annotations are meant more as an internal and provider thing, you may be able to use that as a workaround, yes. Another option is to get the timestamp generated in the databases e.g. via a trigger (doc). /cc @ajcvickers in case he has other ideas. |
Well, currently I can just override |
Also, how can I add annotation to the entity via |
Not sure what you're asking - you seem to already be using PropertyBuilder.HasAnnotation() - IsTimestamp() can be an extension over PropertyBuilder which just does that, no? |
I mean, I add annotation to the property, but I'd like to add it to the entity as well when I call |
You can drop down to the lower-level metadata API and get the property's declaring entity type: modelBuilder.Entity<Blog>().Property(f => f.Id).Metadata.DeclaringType.SetAnnotation(); |
Ok, thanks. |
ping... |
For example, I am developing a NuGet library which should support various DB libraries including EF.
Is there any option to extend data model other than requiring
DbContext
to implement a particular interface or inherit from another context?The text was updated successfully, but these errors were encountered: