[英]Type 'TEntity' doesn't match expected type '???'
我已经建立了一个通用的存储库接口,该接口挂起了许多实体特定的存储库接口。 反映此结构的是许多具体的存储库类。
我的基本存储库类中出现以下错误:
类型“ TEntity”与预期的类型“ ???”不匹配。
方法'GetAll'无法通过接口'... IRepository <TEntity,TIdentity>'实现方法。 返回类型应为“ System.Collections.Generic.List <???>”。
缩小的接口/类结构如下:
IRepository :
public interface IRepository<TEntity, in TIdentity> where TEntity : IEntity<TIdentity>
{
...
List<TEntity> GetAll();
...
}
仓库 :
internal class Repository<TEntity, TIdentity> : IRepository<TEntity, TIdentity>
where TEntity : class, IEntity<TIdentity>
{
...
protected DbSet<TEntity> Set => _set ?? (_set = _context.Set<TEntity>());
public List<TEntity> GetAll()
{
return Set.ToList();
}
...
}
IRoleRepository :
public interface IRoleRepository : IRepository<Role, Guid>
{
...
Role FindByName(string roleName);
...
}
RoleRepository :
internal class RoleRepository : Repository<Role, Guid>, IRoleRepository
{
...
public Role FindByName(string roleName)
{
return Set.FirstOrDefault(x => x.Name == roleName);
}
...
}
这对我的消费类产生了影响,在RoleRepository.GetAll()
,执行RoleRepository.GetAll()
返回List<???>
而不是我期望的List<Role>
。
编辑-实体定义...
IEntity :
public interface IEntity<T>
{
T Id { get; set; }
byte[] Version { get; set; }
}
实体 :
public abstract class Entity<T> : IEntity<T>
{
public T Id { get; set; }
public byte[] Version { get; set; }
}
角色 :
public class Role : Entity<Guid>
{
private ICollection<User> _users;
public string Name { get; set; }
public ICollection<User> Users
{
get { return _users ?? (_users = new List<User>()); }
set { _users = value; }
}
}
看来您的TEntity
类没有实现该接口: IEntity<TIdentity>
取决于您是在EntityFramework
还是在ModelFirst
上使用CodeFirst
,还可以使用其他选项:
ModelFirst
:您可以使用Model.tt
文件指定所需接口的实现。
CodeFirst
:只需在模型类中实现该接口。
第三种选择是,仅当您不使用该接口中的任何内容时,才摆脱where TEntity : IEntity<TIdentity>
的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.