C# 如何解决错误:不一致的可访问性:约束类型';一致性';比';通用存储<;T>';
我已经用菱形符号创建了一个带有以下方法的接口(GetAll、GetById、CreateAsync…等等),我告诉你它是一个T的存储库,其中T是一个类,所以你可以为任何具有 IGENERICREPOSITORY.CS:C# 如何解决错误:不一致的可访问性:约束类型';一致性';比';通用存储<;T>';,c#,asp.net-core,interface,.net-core,asp.net-core-mvc,C#,Asp.net Core,Interface,.net Core,Asp.net Core Mvc,我已经用菱形符号创建了一个带有以下方法的接口(GetAll、GetById、CreateAsync…等等),我告诉你它是一个T的存储库,其中T是一个类,所以你可以为任何具有 IGENERICREPOSITORY.CS: public interface IGenericRepository<T> where T : class { IQueryable<T> GetAll(); Task<T> GetByIdAsy
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
Task<T> GetByIdAsync(int id);
Task<T> CreateAsync(T entity);
Task<T> UpdateAsync(T entity);
Task DeleteAsync(T entity);
Task<bool> ExistAsync(int id);
}
公共接口IGenericRepository,其中T:class
{
IQueryable GetAll();
任务GetByIdAsync(int-id);
任务CreateAsync(T实体);
任务更新同步(T实体);
任务删除异步(T实体);
任务ExistAsync(int-id);
}
但是在实现接口时,在一个名为GenericRepository.cs的类中,它告诉我一个不一致的错误,我附加了我的GenericRepository.cs类
GenericRepository.CS:
public class GenericRepository<T> : IGenericRepository<T> where T : class, IEntity
{
private readonly DataContext context;
public GenericRepository(DataContext context)
{
this.context = context;
}
public IQueryable<T> GetAll()
{
return this.context.Set<T>().AsNoTracking();
}
public async Task<T> GetByIdAsync(int id)
{
return await this.context.Set<T>()
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == id);
}
public async Task<T> CreateAsync(T entity)
{
await this.context.Set<T>().AddAsync(entity);
await SaveAllAsync();
return entity;
}
public async Task<T> UpdateAsync(T entity)
{
this.context.Set<T>().Update(entity);
await SaveAllAsync();
return entity;
}
public async Task DeleteAsync(T entity)
{
this.context.Set<T>().Remove(entity);
await SaveAllAsync();
}
public async Task<bool> ExistAsync(int id)
{
return await this.context.Set<T>().AnyAsync(e => e.Id == id);
}
public async Task<bool> SaveAllAsync()
{
return await this.context.SaveChangesAsync() > 0;
}
}
公共类GenericRepository:IGenericRepository,其中T:class,Entity
{
私有只读数据上下文;
公共GenericRepository(DataContext上下文)
{
this.context=上下文;
}
公共IQueryable GetAll()
{
返回此.context.Set().AsNoTracking();
}
公共异步任务GetByIdAsync(int id)
{
return wait this.context.Set()
.AsNoTracking()
.FirstOrDefaultAsync(e=>e.Id==Id);
}
公共异步任务CreateAsync(T实体)
{
等待这个.context.Set().AddAsync(实体);
等待SaveAllAsync();
返回实体;
}
公共异步任务UpdateAsync(T实体)
{
this.context.Set().Update(实体);
等待SaveAllAsync();
返回实体;
}
公共异步任务DeleteAsync(T实体)
{
this.context.Set().Remove(实体);
等待SaveAllAsync();
}
公共异步任务ExistAsync(int-id)
{
return wait this.context.Set().AnyAsync(e=>e.Id==Id);
}
公共异步任务SaveAllAsync()
{
return wait this.context.saveChangesSync()>0;
}
}
错误如下:
可访问性不一致:约束类型“IEntity”的可访问性不如“GenericRepository”
我做错了什么?为什么我不能实现GenericRepostory.cs类?我想我所有的用途和继承都是正确的,对我有帮助吗?那是原因类型
IEntity
是用非公共访问修饰符声明的,而GenericRepository
是用public
那是原因类型IEntity
是用非公共访问修饰符声明的,而GenericRepository
是用public
声明的
约束类型“IEntity”的可访问性不如
“一般保存”
这意味着IEntity
与GenericRepository
没有相同(或更广泛)的可访问性。编译器给出此错误是因为它会导致不一致,例如,如果您甚至不能创建一个IEntity
传递给它,您将如何调用您的updateSync
要解决此问题,您可以将IEntity
公开
,或者将对通用存储
的访问限制在与IEntity
相同的级别(例如内部
,如果IEntity
被标记为这样),具体取决于您的预期用例
约束类型“IEntity”的可访问性不如
“一般保存”
这意味着IEntity
与GenericRepository
没有相同(或更广泛)的可访问性。编译器给出此错误是因为它会导致不一致,例如,如果您甚至不能创建一个IEntity
传递给它,您将如何调用您的updateSync
要解决此问题,您可以将
IEntity
公开
,或者将对通用存储
的访问限制在与IEntity
相同的级别(例如内部
,如果IEntity
被标记为这样),取决于您的预期用例。最有可能的是IEntity
不是公共接口,例如,它可能是内部接口。您能同时显示该接口的声明吗?很可能IEntity
不是公共接口,例如,它可能是内部接口。您能同时显示该接口的声明吗?