You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to use dependency injection on a custom value converter?
In my scenario I have dynamic connection string that changes the db based on an api controller route path.
In a segment of that path we store the db alias and we have services in places that adjust the connection string according to that value.
Now the problem is that one of the model has a property that transform a local table identification (autonumeric id) into a string that's composed of both the "database alias" and the local resource id.
E.g. we have a table TableX with it's property ID = 5 on a database named "MyDatabase", the ending result should be
a string property with value "MyDatabase-5"
To do this I tried to implement a ValueConverter using our service that collect the db alias and composing the final value in this ValueConverter.
I thought value converter were instantiated by a serviceProvider using DI but it seems it's not the case, infact I get an error
Cannot create an instance of value converter type 'DomainEntityIdToLocalIdConverter'. Ensure that the type can be instantiated and has a parameterless constructor, or use the overload of 'HasConversion' that accepts a delegate
The value converter I did is this
publicclassDomainEntityIdToLocalIdConverter:ValueConverter<DomainEntityId?,long?>{publicDomainEntityIdToLocalIdConverter(IWayDomainDataContextwayDomainDataContext):base(
v =>v==null?null:v.RemoteId,
v =>v==null?null:wayDomainDataContext.GetDomainEntityId(v.Value)){}}
Does ValueConverter supports any kind of dependency injection mechanism?
If not (sigh) what's the suggested approach to achieve what we need?
P.S.
The scope of our service is "Scoped" so matches the EFCore DBContext scope
@MithrilMan Value converters are part of the model, so this would require building a new model each time the converter changes. This could be solved by #12205, which we are considering for EF8. As it stands, I can't think of a way to do it short of rebuilding the model, which is not very efficient, but could be an option if the number of databases is not too large. See https://learn.microsoft.com/en-us/ef/core/modeling/dynamic-model
Is it possible to use dependency injection on a custom value converter?
In my scenario I have dynamic connection string that changes the db based on an api controller route path.
In a segment of that path we store the db alias and we have services in places that adjust the connection string according to that value.
Now the problem is that one of the model has a property that transform a local table identification (autonumeric id) into a string that's composed of both the "database alias" and the local resource id.
E.g. we have a table TableX with it's property ID = 5 on a database named "MyDatabase", the ending result should be
a string property with value "MyDatabase-5"
To do this I tried to implement a ValueConverter using our service that collect the db alias and composing the final value in this ValueConverter.
I thought value converter were instantiated by a serviceProvider using DI but it seems it's not the case, infact I get an error
The value converter I did is this
Does ValueConverter supports any kind of dependency injection mechanism?
If not (sigh) what's the suggested approach to achieve what we need?
P.S.
The scope of our service is "Scoped" so matches the EFCore DBContext scope
Thanks
EF Core version: 6.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
The text was updated successfully, but these errors were encountered: