0

My main goal is to override the DeleteObject method of ObjectSet. I'm using EntityFramework 6 with an ObjectContext that I migrated from EF 4.x. EDIT: I modified the T4 template that created the ObjectContext to associate all the ObjectSets on the context to use MyObjectSet instead of the standard, but I want to use it the same way: context.HijackedSet.Where(i=> i.blah == 'blah')

EDIT: I am only having issues with nested ObjectSet calls such as

(from p in context.SomeTable
let poco = new MyExcellentPOCO
{
   Name = p.Name,
   OtherProperty = context.UnrelatedTable.Where(i=> i.LimitingFactor = x)
}
select poco).ToList()

Unfortunately ObjectSet uses an internal constructor I was not able to inherit from it. Instead I created my own version with an ObjectSet property, and passes through to the property for all of the ObjectSet Methods

Code:

public class MyObjectSet<TEntity> : IObjectSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable where TEntity : class
{
    public MyObjectSet(ObjectSet<TEntity> objectSet)
    {
        ObjectSet = objectSet;
    }

    public ObjectSet<TEntity> ObjectSet { get; set; }

    public void DeleteObject(TEntity entity)
    {
        if (entity is IEnforceLogicalDelete)
        {
            ((IEnforceLogicalDelete)entity).IsDeleted = true;
        }
        else
        {
            ObjectSet.DeleteObject(entity);
        }
    }

    public void AddObject(TEntity entity)
    {
        ObjectSet.AddObject(entity);
    }

    public void Attach(TEntity entity)
    {
        ObjectSet.Attach(entity);
    }

    public void Detach(TEntity entity)
    {
        ObjectSet.Detach(entity);
    }

    public IEnumerator<TEntity> GetEnumerator()
    {
        return ((IEnumerable<TEntity>)ObjectSet).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<TEntity>)ObjectSet).GetEnumerator();
    }

    public Type ElementType
    {
        get { return typeof(TEntity); }
    }

    public Linq.Expressions.Expression Expression
    {
        get
        {
            return ObjectSet.AsQueryable<TEntity>().Expression;
        }
    }

    public IQueryProvider Provider
    {
        get { return ObjectSet.AsQueryable<TEntity>().Provider; }
    }

    public ObjectQuery<TEntity> Include(string path)
    {
        return ObjectSet.Include(path);
    }        
}

MyObjectSet as it is implemented on the context:

public MyObjectSet<Address> Addresses
{
    get
    {
        if ((_Addresses == null))
        {
            _Addresses = new MyObjectSet<Address>(base.CreateObjectSet<Address>("Addresses"));
        }
        return _Addresses;
    }
}
private MyObjectSet<Address> _Addresses;

I inspected the ObjectQuery class (ObjectSet inherits from ObjectQuery) and it implements IEnumerator IEnumerable.GetEnumerator() as return ((IEnumerable<T>)this).GetEnumerator();. Does my use of the property change the result? return ((IEnumerable<TEntity>)ObjectSet).GetEnumerator();

Noel
  • 600
  • 16
  • 37
  • This 'My main goal is to override the DeleteObject method of ObjectSet.' seems like a means a not a goal. What are you trying to achieve? It might be much easier to achieve what you want by overriding `.SaveChanges()` – Pawel Jan 03 '14 at 19:57
  • @Pawel We did that originally, but since we are doing all logical deletes we were finding that when using transactions that spanned multiple contexts we were having trouble assigning the original values back to the deleted entities. Creating a new DeleteObject method on the context that set an IsDeleted flag on entities instead of marking them as deleted worked, but wanted to have the same behavior on context.entities.DeleteObject – Noel Jan 03 '14 at 20:13

0 Answers0