GraphDiff
GraphDiff copied to clipboard
Associate entity within a inherited entity
First off, great work with graphdiff, it really makes a different when developing webpages and working with detached entities.
I've come across a issue which I'm not able to figure how to solve. I've been looking all around on the internet and the documentation I've found without finding any clues on how to solve it. I'm trying to associate a entity that is a property to a entity that is inheriting from another entity. Let me show with code:
Project class:
public class Project
{
public Guid Id { get; set; }
public virtual List<Activity> Activities { get; set; }
}
Activity class:
public class Activity
{
public Guid Id { get; set; }
public String Name { get; set; }
public virtual Company Company { get; set; }
}
SurfingActivity class:
public class SurfingActivity : Activity
{
public String Comment { get; set; }
public virtual Weather Weather { get; set; }
}
The update context using graphdiff:
public void UpdateActivity(Project project)
{
this.UpdateGraph(project, map => map
.OwnedCollection(p => p.Activities, with => with
.AssociatedEntity(a => a.Company)
)
);
}
The last section here sets the association, and I'm not able to set the association to Weather in SurfingActivity. It there any way to solve this with the current structure of code? If it's not supported yet, will it be supported? I can set Id, Name, Company and Comment and it all saves but not weather.
Hi,
I'm afraid this is currently neither supported via the fluent API nor the attribute based mapping. I think it's a (common?) use case so I might try to implement support for it as soon as I have some spare time. Of course you're always free to send a pull request.. :)
Thanks for GraphDiff, without it EF is a drag in disconnected scenarios. I ran into something similar today, I have a collection of polymorphic types, each of which may have its own collection of polymorphic types. Not know what objects are coming through makes it hard to configure. I just want everything to be treated as an OwnedCollection.
Some magic call like this :) TfContext.UpdateGraph(entity, config => config.WalkDownFromParent().TreatAllAsOwnedCollections())
For those of you looking for a work-around until this is implemented, I've found the following to work, though I have not tested it extensively yet. Using @ArneBK 's example code, change Project to the following:
public class Project
{
public Guid Id { get; set; }
public virtual List<Activity> Activities { get; set; }
public ICollection<SurfingActivity> SurfingActivities => Activities?.OfType< SurfingActivity>().ToList();
}
And the update to:
public void UpdateActivity(Project project)
{
this.UpdateGraph(project, map => map
.OwnedCollection(p => p.Activities, with => with
.AssociatedEntity(a => a.Company)
)
.OwnedCollection(p => p.SurfingActivities, with => with
.AssociatedEntity(a => a.Weather)
)
);
}
FYI, I have not tested this exact situation. In my situation, the extended 'Activity' class has its own OwnedCollection
rather than an AssociatedEntity
but GraphDiff appears to treat the extra OwnedCollection
exactly as it should (adding and removing appropriate collection members) and does not appear to misbehave by having the extra SurfingActivities
collection.
@andypelzer do you have any comment on this approach for now?