SubObjectSet

With EF, when you use TPH or TPC inheritance mapping scenarii, the EntitySet is on the base class.


As I mentioned often in the past with EF v1, you can add a property in your context which returns the EntitySet.OfType<MySubType>().


Ok it’s interesting but… In EF v1, the EntitySet is an ObjectQuery<T> property and our property also but in EF v2 the EntitySet is an ObjectSet<T>. This class implements the IObjectSet<T> interface which has three methods to add, attach and delete entities.


One guy tells me that he wants to be able to use these methods directly on the “sub EntitySet” property.


To realize it, I made the following class:


public class SubObjectSet<TBase, TInherited> : ObjectQuery<TInherited>, IObjectSet<TInherited>

    where TBase : class

    where TInherited : class, TBase

{

    public ObjectSet<TBase> ObjectSet { get; private set; }

 

    public SubObjectSet(ObjectSet<TBase> objectSet)

        : base(objectSet.OfType<TInherited>().CommandText, objectSet.Context)

    {

        ObjectSet = objectSet;

    }

 

    #region IObjectSet<TInherited> Members

    public void AddObject(TInherited entity)

    {

        ObjectSet.AddObject(entity);

    }

    public void Attach(TInherited entity)

    {

        ObjectSet.Attach(entity);

    }

    public void DeleteObject(TInherited entity)

    {

        ObjectSet.DeleteObject(entity);

    }

    #endregion

}

This entry was posted in 7671, 7674. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>