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
However, when I tried to set valueconverters for every Strongly-Typed-Id properties at runtime, the code is as following:
Step one: Remove "builder.Property(p => p.Id).HasConversion(id => id.Value, value => new CategoryId(value));" from CategoryConfig
Step Two:
publicstaticclassStronglyTypedIdHelper{publicstaticvoidAddStronglyTypedIdConversions(thisModelBuildermodelBuilder){foreach(var entityType in modelBuilder.Model.GetEntityTypes()){if(entityType.IsOwned()){continue;}foreach(var property in entityType.GetProperties()){TypepropertyType= property.ClrType;if(IsStronglyTypedId(propertyType)){TypevalueType=typeof(Guid);varconverter= StronglyTypedIdConverters.GetOrAdd(
propertyType,_ => CreateStronglyTypedIdConverter(propertyType, valueType));
property.GetValueConverter();
property.SetValueConverter(converter);}}}}privatestaticboolIsStronglyTypedId(Typetype){return type.IsAssignableTo(typeof(StronglyTypedId));}privatestaticreadonlyConcurrentDictionary<Type,ValueConverter>StronglyTypedIdConverters=new();privatestatic ValueConverter CreateStronglyTypedIdConverter(TypestronglyTypedIdType,TypevalueType){// id => id.ValuevartoProviderFuncType=typeof(Func<,>).MakeGenericType(stronglyTypedIdType, valueType);varstronglyTypedIdParam= Expression.Parameter(stronglyTypedIdType,"id");vartoProviderExpression= Expression.Lambda(
toProviderFuncType,
Expression.Property(stronglyTypedIdParam,"Value"),
stronglyTypedIdParam);// value => new ProductId(value)varfromProviderFuncType=typeof(Func<,>).MakeGenericType(valueType, stronglyTypedIdType);varvalueParam= Expression.Parameter(valueType,"value");varctor= stronglyTypedIdType.GetConstructor(new[]{ valueType });varfromProviderExpression= Expression.Lambda(
fromProviderFuncType,
Expression.New(ctor, valueParam),
valueParam);varconverterType=typeof(ValueConverter<,>).MakeGenericType(stronglyTypedIdType, valueType);return(ValueConverter)Activator.CreateInstance(
converterType,
toProviderExpression,
fromProviderExpression,null);}}
Step Three: invoke AddStronglyTypedIdConversions in OnModelCreating
The code above cannot work properly, and exception 'The entity type XXXId requires a primary key to be defined.' will be thrown.
Because entityType.GetProperties() only returns scalar properties, the Id property of Category is not included in the returned values of entityType.GetProperties(). I can get the Id property of Category with entityType.GetNavigations(), but IMutableNavigation cannot invoke the SetValueConverter method.
.NET Version:.NET 5
EF Core version:5.0.9
My questioin is :how can I set ValueConverter for non-scalar property at runtime?
Thanks.
The text was updated successfully, but these errors were encountered:
yangzhongke
changed the title
A bug of EF Core with owned Type and SetValueConverter at runtime.
A problem with owned Type and SetValueConverter at runtime.
Aug 16, 2021
In 6.0.0-preview6 we shipped #10784 which allows you to call .Properties<CategoryId>().HaveConversion<CategoryIdConverter>()
Thanks for your replies. However, "Properties().HaveConversion()" cannot solve my problem, because what I want to do is to SetValueConverter at runtime.
foreach(var navigation in entityType.GetNavigations()){TypepropertyType= navigation.ClrType;if(IsStronglyTypedId(propertyType)){TypevalueType=typeof(Guid);varconverter= StronglyTypedIdConverters.GetOrAdd(
propertyType,_ => CreateStronglyTypedIdConverter(propertyType, valueType));
navigation.SetValueConverter(converter);//error: no such method}}
I tried to implement Strongly-Typed-Id in EF Core.
The following code works well:
However, when I tried to set valueconverters for every Strongly-Typed-Id properties at runtime, the code is as following:
Step one: Remove "builder.Property(p => p.Id).HasConversion(id => id.Value, value => new CategoryId(value));" from CategoryConfig
Step Two:
Step Three: invoke AddStronglyTypedIdConversions in OnModelCreating
The code above cannot work properly, and exception 'The entity type XXXId requires a primary key to be defined.' will be thrown.
Because entityType.GetProperties() only returns scalar properties, the Id property of Category is not included in the returned values of entityType.GetProperties(). I can get the Id property of Category with entityType.GetNavigations(), but IMutableNavigation cannot invoke the SetValueConverter method.
.NET Version:.NET 5
EF Core version:5.0.9
My questioin is :how can I set ValueConverter for non-scalar property at runtime?
Thanks.
The text was updated successfully, but these errors were encountered: