0

I need to have a CRUd operations on my class (CompetenceSpecific). Competence has three derived classes - CompetenceFunction, CompetenceArea and CompetenceSpecifc

The error I recieved: 
  There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity

How should I correct this? Please suggest a solution that would solve my problem. Thanks

Please check the code below, I removed some parts of the code for simplicity.

--MODEL

public class Competence
{
    public int CompetenceID { get; set; }
    public int CourseID { get; set; }
...
}

public class CompetenceFunction : Competence
{

}

--REPOSITORY and interfaces

public interface IRepository<T> where T : class
{
    T GetById(object id);
    IEnumerable<T> GetAll();
    IEnumerable<T> Query(Expression<Func<T, bool>> filter);
    void Add(T entity);
    void Remove(T entity);
}

public abstract class Repository<T> : IRepository<T>
    where T : class
{
    protected IObjectSet<T> _objectSet;

    public Repository(ObjectContext context)
    {
        _objectSet = context.CreateObjectSet<T>();
    }
...
}


public class CompetenceFunctionRepository : Repository<CompetenceFunction>
{
    public CompetenceFunctionRepository(ObjectContext context)
        : base(context)
    {
    }

    public override CompetenceFunction GetById(object id)
    {
        return _objectSet.SingleOrDefault(s => s.CompetenceID == (int)id);
    }
}

--UNIT oF WORK

public interface IUnitOfWork
{
    IRepository<CompetenceFunction> CompetenceFunctions { get; }
    IRepository<CompetenceArea> CompetenceAreas { get; }
    IRepository<CompetenceSpecific> CompetenceSpecifics { get; }
    void Commit();
}

public class UnitOfWork : IUnitOfWork, IDisposable
{
    private CompetenceFunctionRepository _competencefunction;
    private CompetenceAreaRepository _competencearea;
    private CompetenceSpecificRepository _competencespecifc;

    public UnitOfWork(ObjectContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("Context was not supplied");
        }

        _context = context;
    }

    #region IUnitOfWork Members


    public IRepository<CompetenceFunction> CompetenceFunctions
    {
        get
        {
            if (_competencefunction == null)
            {
                _competencefunction = new CompetenceFunctionRepository(_context);
            }

            return _competencefunction;
        }
    }

    public IRepository<CompetenceArea> CompetenceAreas
    {
        get
        {
            if (_competencearea == null)
            {
                _competencearea = new CompetenceAreaRepository(_context);
            }

            return _competencearea;
        }
    }

    public IRepository<CompetenceSpecific> CompetenceSpecifics
    {
        get
        {
            if (_competencespecifc == null)
            {
                _competencespecifc = new CompetenceSpecificRepository(_context);
            }

            return _competencespecifc;
        }
    }

--Im getting an error in this part of Repository

public Repository(ObjectContext context)
    {
        _objectSet = context.CreateObjectSet<T>();
    }


  There are no EntitySets defined for the specified entity type 'CompetencyManagement.Domain.Entities.CompetenceFunction'. If 'CompetencyManagement.Domain.Entities.CompetenceFunction' is a derived type, use the base type instead. Parameter name: TEntity

Here's how I implement in the controller

 private IUnitOfWork _unitOfWork;
 var a = _unitOfWork.CompetenceFunctions.GetAll();
 return View(a);
samantha07
  • 507
  • 1
  • 7
  • 21

1 Answers1

2

You have to get derived type by the OfType function, e.g.

context.CreateObjectSet<Competence>().OfType<CompetenceFunction>()

In your case that would mean that there is only a CompetenceRepository that serves all derivatives of Competence.


Edit

(After your comment)
First, UoW is meant for temporarily storing changes that should be dealt with in one batch (like changes to be committed to the database). GetAll and similar functions are repository stuff.

But do you need repositories? I like this post. When beginning to know EF, I would focus on the ins and outs of EF without getting distracted too much by surrounding architecture. E.g. start with services that at the inside communicate directly with the context and expose methods like GetCompetenceFunctions, GetCompetenceAreas (using OfType), and SaveCompetenceFunction, ....

You can address these service methods directly from action methods in the MVC controllers.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thanks GertArnold. How can I implement this on my current design? Sorry I'm only a beginner at this. see updated question above. Thanks – samantha07 Jul 16 '12 at 17:11