我试图松散地耦合这段代码,但我不知道该如何或者应该如何。与实体框架的松散耦合
我正在使用实体框架,而DbContext
是实体对象TMeasure
使用的继承类。当我运行此代码时,出现此错误:
'System.Data.Entity.DbContext' does not contain a definition for 'TMeasures' and no extension method 'TMeasures' accepting a first argument of type 'System.Data.Entity.DbContext' could be found (are you missing a using directive or an assembly reference?)
有人可以帮助我吗?
谢谢!
class MeasureRepository: IMeasureRepository
{
private DbContext db;
public MeasureRepository(DbContext db)
{
this.db = db;
}
public List<TMeasure> GetAll()
{
var results = (from i in db.TMeasures
orderby i.strMeasure
select i).ToList();
return results;
}
}
我不能用什么方式,这可能被视为看“松散耦合”,它似乎破碎了。你最终的目标是什么?你想编写一个可以与任何DbContext一起工作的repo,而不是从DbContext派生的特定类。如果是谷歌的“通用知识库”,我知道我已经发布过去的例子。 – 2014-10-28 16:27:34
Mati(下图)为我解决了我的问题。它显然已经坏了,但我说的是不必在类中实例化一个实体类。我宁愿将实体对象传递给类本身。 – 2014-10-29 14:31:34