0

我需要对我的班级进行 CRUd 操作(CompetenceSpecific)。Competence 具有三个派生类 - CompetenceFunction、CompetenceArea 和 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

我应该如何纠正这个?请提出一个可以解决我的问题的解决方案。谢谢

请检查下面的代码,为简单起见,我删除了部分代码。

- 模型

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

public class CompetenceFunction : Competence
{

}

--存储库和接口

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);
    }
}

--工作单位

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;
        }
    }

--我在存储库的这一部分中遇到错误

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

这是我在控制器中实现的方式

 private IUnitOfWork _unitOfWork;
 var a = _unitOfWork.CompetenceFunctions.GetAll();
 return View(a);
4

1 回答 1

2

您必须通过OfType函数获取派生类型,例如

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

在您的情况下,这意味着只有一个 CompetenceRepository 服务于 Competence 的所有衍生物。


编辑

(在您发表评论之后)
首先,UoW 用于临时存储应该在一批中处理的更改(例如要提交到数据库的更改)。GetAll和类似的功能是存储库的东西。

但是你需要存储库吗?我喜欢这篇文章。当开始了解 EF 时,我会专注于 EF 的来龙去脉,而不会被周围的架构分心。例如,从内部直接与上下文通信的服务开始,并公开 GetCompetenceFunctions、GetCompetenceAreas(使用OfType)和 SaveCompetenceFunction 等方法,...。

您可以直接从 MVC 控制器中的操作方法处理这些服务方法。

于 2012-07-15T23:47:11.507 回答