【发布时间】:2014-03-18 18:20:41
【问题描述】:
我正在尝试实现一个通用存储库,但它失败了,因为没有从 DBML 对象到通用实体的隐式转换。我不知道如何让 DBML 对象从 IEntity 继承,或者这是否是解决方案。
以下是我正在使用的接口和存储库签名。
public interface IEntity
{
int ID { get; }
}
public interface IRepository<T> : IDisposable
{
....
}
public class Repository<T> : IRepository<T> where T : class, IEntity
{
....
}
这是模型类。它在第 9 行失败并出现错误(如下),其中存储库类的实例在构造函数中被实例化。这只是在我将 IEntity 约束添加到 Repository 类之后才开始发生的。
public class MyModel
{
DataContext DC;
Repository<MyType> MyRep;
public MyModel()
{
DC = new DataContext("ConnStr");
MyRep = new Repository<MyType>(DC);
}
}
这是错误:该类型不能用作泛型类型或方法中的类型参数“T”。没有从“MyType”到“IEntity”的隐式引用转换
提前感谢您的帮助。
【问题讨论】:
标签: c# entity-framework generics