2012-07-15 165 views
0

我需要對我的類(CompetenceSpecific)進行CRUd操作。 競爭力有三個派生類 - CompetenceFunction,CompetenceArea和CompetenceSpecifc實體框架:ObjectContext和繼承

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 

我應該如何糾正呢?請提出解決方案,以解決我的問題。謝謝

請檢查下面的代碼,爲簡單起見,我刪除了部分代碼。

--model

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

public class CompetenceFunction : Competence 
{ 

} 

--REPOSITORY和接口

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工作

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獲得在庫

的這部分的錯誤
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); 

回答

2

你必須得到由OfType功能派生類型,例如如何實現

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

在你的情況下,這意味着只有一個CompetenceRepository服務於Competence的所有衍生產品。


編輯

(您的評論後)
首先,UOW是指用於暫時存儲應在一個批次進行處理(如被提交到數據庫的更改)。 GetAll和類似的功能是存儲庫的東西。

但您是否需要知識庫?我喜歡this post。當開始瞭解EF時,我會關注EF的來龍去脈,而不會被周圍的架構過分分散注意力。例如。從內部直接與上下文進行通信並公開諸如GetCompetenceFunctions,GetCompetenceAreas(使用OfType)和SaveCompetenceFunction,...的方法。

您可以直接從MVC控制器中的操作方法處理這些服務方法。

+0

謝謝GertArnold。我怎樣才能在我目前的設計上實現這一點?對不起,我只是這個初學者。看到上面更新的問題。謝謝 – samantha07 2012-07-16 17:11:12

+0

請參閱我的編輯。 – 2012-07-16 18:23:31