Updating a property with no setter rather than replacing it (EF Core)
I'm using EF Core. One of my properties on my Resource entity is like this:
public virtual ICollection<ResourceType> ResourceTypes { get; } = new List<ResourceType>();
I've set ValueMappers, data types, and configurator to allow me to set ResourceTypes correctly in the UI. However the property doesn't get updated because it has no setter. What I want Konstrukt to do is to update the ICollection and not replace it. I see no way to achieve this other than to have a fake duplicate ResourceTypes property and using an event/custom repo to move sync the two properties. If feels hacky.
Is there no other way to achieve this? Ideally the original property would be provided in the ValueMapper and I could then update this rather than return a new object.
If anyone else has this issue, then please know that my solution was to create a new model for Konstrukt (in my case):
// Must be a super class of Resource for the ReplaceVisitor to work
public class ResourceModel : Resource
{
public readonly Resource Resource;
public ResourceModel (Resource resource)
{
// Copy properties
Id = resource.Id;
Name = resource.Name;
}
// a new property that then allows for setting
public new ICollection<ResourceType> ResourceTypes {
get => Resource.ResourceTypes;
set => UpdateCollectionContents(Resource.ResourceTypes, value);
}
public static void UpdateCollectionContents<TEntity> (ICollection<TEntity> collection, IEnumerable<TEntity> newContents)
{
collection.Clear();
foreach (var entity in newContents)
{
collection.Add(entity);
}
}
}
However that then requires that you have a custom repository (which I already did). Unfortunately all the Expressions for where and order by are then refer to your new model rather than your entity. So you need to do some magic to change the input type of your expression:
I'm being prompted for a solution by the forum. I'm going to mark my post above as such, but it would be ideal if in the future Konstrukt could update ICollection properties rather than attempt to overwrite it.
Updating a property with no setter rather than replacing it (EF Core)
I'm using EF Core. One of my properties on my
Resource
entity is like this:I've set ValueMappers, data types, and configurator to allow me to set
ResourceTypes
correctly in the UI. However the property doesn't get updated because it has no setter. What I want Konstrukt to do is to update theICollection
and not replace it. I see no way to achieve this other than to have a fake duplicateResourceTypes
property and using an event/custom repo to move sync the two properties. If feels hacky.Is there no other way to achieve this? Ideally the original property would be provided in the ValueMapper and I could then update this rather than return a new object.
If anyone else has this issue, then please know that my solution was to create a new model for Konstrukt (in my case):
However that then requires that you have a custom repository (which I already did). Unfortunately all the Expressions for where and order by are then refer to your new model rather than your entity. So you need to do some magic to change the input type of your expression:
I'm being prompted for a solution by the forum. I'm going to mark my post above as such, but it would be ideal if in the future Konstrukt could update ICollection properties rather than attempt to overwrite it.
is working on a reply...