Non-silverlight version of Entity Tools?

Nov 1, 2011 at 2:18 PM

Is there a library that provides functionality similar to Entity Tools, but for server-side, code-first entity framework, rather than Silverlight RIA entity framework?

I just want to do a deep copy from one entity to another.

Nov 1, 2011 at 3:27 PM


With respect to EntityGraph/DataValidationFramework, it is not specific to Silverlight/RIA.

EntityGraph is structured in two parts: EntityGraph.Silverlight and EntityGraph.Net (the same is true for DataValidationFramework).

This means that you can use the .Net parts for server-side, code first EF.

The Silverlight versions are more function complete though, but if you look at the implementation, you'll see that it is pretty straight forward to implement .Net/EF counter parts

Nov 1, 2011 at 6:10 PM

Are you saying EntityGraph can be used to perform a deep copy of a code-first Entity Framework object?

Nov 1, 2011 at 8:53 PM

Yes, that is correct.

EntityGraph.Net is the framework-independent base of EntityGraph. It contains the basic data structures and operations. It is not dependent or specific to Silverlight/RIA, EF, or whatever.

EntityGraph.Silverlight is specific for the RIA framework. This means that we can make certain assumptions about the elements in the graphs (e.g., that they all have Entity as base class). We can therefore implement EntityGraph methods that make use of the RIA-specific features (.e.g, a HasChanges property on a full entity graph rather than on a single entity). Also the Clone and Copy methods that create a deep copy/clone are Ria sepcific because copying the data members of a Ria entity is Ria-specific.

The idea is that for other frameworks (like EF), you can directly make use of the EntityGraph.Net library, or you can  develop a framework-specific library (like EntityGraph.Silverlight). For instance, you could make an EntityGraph.EF with EF-specific EntityGraph methods.

The Ria-specific implementation of the Copy method looks like:

public Entity Copy()
    return GraphMap(CopyDataMembers);

where CopyDataMembers creates a new instance of a single entity and copies its data members. GraphMap takes care of visiting the entities in a graph and setting the correct associations.

To make a similar Copy method for EF you would provide a different implementation of CopyDataMembers that will typically consult EF's MetadataWorkspace to find the properties of each entity that should be copied. It will look something like:

TCopy CopyDataMembers<TCopy>(MetadataWorkspace metadataWorkspace, TCopy source) where TCopy : class
    var entityType = source.GetType();

    // Create new object of type TCopy (or subtype) using reflection and inspecting the concrete
    // type of the entity to copy.
    TCopy copy = (TCopy)Activator.CreateInstance(entityType);

    var edmType = metadataWorkspace.GetItems<EntityType>(DataSpace.CSpace).Single(type => type.Name == entityType.Name);

    foreach(var prop in edmType.Properties.Where(p => edmType.KeyMembers.Contains(p) == false))
        var propInfo = entityType.GetProperty(prop.Name);
        propInfo.SetValue(copy, propInfo.GetValue(source, null), null);
    return copy;

The only thing is that to get it working with code-first you need to supply the MetadataWorkspace. So your copy
method would typically require this as its single method argument.

Defining an EntityGraph shape is the same for RIA and non-Ria and looks something like this:
var shape = new EntityGraphShape()
    .Edge<T1,T2>(x => x.PropertyOfTypeT2)
    .Edge<T2,T3>(x => x.PropertyOfTypeT3)

I Hope this helps.