2

The ObjectSet.Single(predicate) doesn't work (the Where() and toList() methods as well) unless i write it this way:

ObjectSet.Cast<TEntity>().Single<TEntity>(predicate)

But i don't know what to do to resolve the problem with the AddObject and DeleteObject methods:

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

The error message tells me that "entity" is a wrong argument. Is the problem related to EF 4.1?

Rahma
  • 255
  • 3
  • 21
  • Can you show an example of the code in use? I think knowing what you are passing in as the predicate is important in order to understand the issue. – Brian Dishaw Aug 04 '11 at 11:54
  • I didn't pass any predicate in yet. The method is underlined red after i finish writing it. – Rahma Aug 04 '11 at 12:20

1 Answers1

2

Here are a few snippets from my generic repository:

public void Add<K>(K entity) where K : class
{            
    context.CreateObjectSet<K>().AddObject(entity);
}

public K SingleOrDefault<K>(Expression<Func<K, bool>> predicate) where K : class
{
    K entity = context.CreateObjectSet<K>().SingleOrDefault<K>(predicate);

    return entity;
}

Please see the link below: http://msdn.microsoft.com/en-us/library/dd382944.aspx

Edit: If you already have a created ObjectSet then your class already defines TEntity, therefor your method should be adjusted as such:

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

You should also be able to make similar adjustment to your Single() method, there should be no need for a cast.

e36M3
  • 5,952
  • 6
  • 36
  • 47
  • 1
    Understood, adjust your Add method to use TEntity as defined by the class template. You don't need the right after the method name. – e36M3 Aug 04 '11 at 12:17