-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mechanism/API to specify a default conversion for any property of a given type in the model #10784
Comments
Note that the converter isn't really for "every property" even though it will also have that effect. It should also be for any time the CLR type is used in the query--that is, it is really a custom type mapping setup by the application. |
As a temporary workaround you can use this extension method(I think it works): public static class ModelBuilderExtensions
{
public static ModelBuilder UseValueConverterForType<T>(this ModelBuilder modelBuilder, ValueConverter converter)
{
return modelBuilder.UseValueConverterForType(typeof(T), converter);
}
public static ModelBuilder UseValueConverterForType(this ModelBuilder modelBuilder, Type type, ValueConverter converter)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
// note that entityType.GetProperties() will throw an exception, so we have to use reflection
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == type);
foreach (var property in properties)
{
modelBuilder.Entity(entityType.Name).Property(property.Name)
.HasConversion(converter);
}
}
return modelBuilder;
}
} |
@ZeroNightzz What exception is thrown by |
this is caused, so I have to use |
@Necronux Thanks. I thought you were indicating an exception when calling the GetProperties method itself. |
The bulk configuration should be performed before set discovery as it could affect whether a property would be considered a navigation. See #12229 |
@AndriySvyryd Could you explain how to configure EF Core to use a default conversion for any property of a given type in the model? |
@vanillajonathan this new feature was just merged, and will only be available in 6.0 previews. For 5.0, there's a good chance you can do something similar to #10784 (comment) as a workaround. |
For EF Core 6.0.0-preview6: protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<string>().HaveConversion<byte[]>()
} |
Hi @AndriySvyryd , I'm tring to use the HaveConversion method to pass in the two transofmration lambdas ala configurationBuilder.Properties().HaveConversion(c=>c.ToString(),s=>Color.FromName(s)); but the compiler is complaining. I looked at source and don't see an overload that matches the signature I use to do this with HasConversion:
Is this coming in EF Core 6? Thanks NOTE: I can achieve this by creating my own ValueConverter class. it's just the handy shortcut that's missing for now. :) |
@julielerman You need to create an actual ValueConverter class when using it for all properties like this. This isn't something that is planned to change. |
Thanks. Yeah I figured that out (I think I wrote my edit at the same time ou wrote this response. LOL. The expression overload would certainly be handy though....someday. |
@julielerman That overload wouldn't be compatible with compiled models, so we discourage using it (by not providing it in the new API) |
ohhh! Right. Now have to keep compiled models in mind for all the configs! Thanks @AndriySvyryd |
The value conversions article needs to be updated. |
As @vanillajonathan outlined, this really should go into the value conversions article for documentation purposes. The only mention to that addition I could find was the |
@Dynalon @vanillajonathan I've added a bulk configuration section to the value conversions page in dotnet/EntityFramework.Docs#3716. |
Wrong link? #3716 doesn't seem to be related to this.. |
Sorry, wrong repo - corrected above. |
Is there a decent workaround for efcore versions < 6? We dont have the ability to upgrade to .net 6 just yet, and have a domain model of thousands of tables that have used the old oracle convention of using bool as char(1) with constraints to Y/N. Something a value converter exists for, but is gonna make mapping this entities a royal nightmare. |
@ronnyek take a look at Bulk configuration in OnModelCreating, that may work for you. Note also that EF Core 5.0 goes out of support in May, and 3.1 in December - so I'd recommend upgrading soon instead. |
Value conversions were introduced by #242. Currently conversions are only set per-property, although bulk configuration can be used at the end of OnModelCreating in the normal way. This issue is about adding some kind of mechanism to set the conversion for all properties of a given type in the model. This could be through configuration on the model, or it could be via improved bulk config APIs.
The text was updated successfully, but these errors were encountered: