【发布时间】:2020-09-28 14:00:18
【问题描述】:
Net core 和 EF Core DB 优先方法。我有一张表,它的 Id 是主键,还有一个 countryname 是非主键。我想查询国家名称,我不会传递主键。下面是我的实现。
IBaseRepository.cs
public interface IBaseRepository<T>
{
async Task<T> GetCountryByNameAsync(string country)
}
BaseRepository.cs
public class BaseRepository<T>: IBaseRepository<T>
where T : class
{
private readonly DbContext dbContext;
private readonly DbSet<T> dbSet;
public BaseRepository(DbContext dbContext)
{
this.dbContext = dbContext;
this.dbSet = dbContext?.Set<T>();
}
public async Task<T> GetCountryByNameAsync(string country)
{
return await this.dbSet.FindAsync(id).ConfigureAwait(false); //This will return based on the countryId
//I want select * from country where countryname = country
}
}
在上述实现中,我可以通过 id(主键)获取国家/地区,但我想根据国家/地区名称进行查询。我试图弄清楚。有人可以指导我如何做到这一点。任何帮助将不胜感激。谢谢
【问题讨论】:
标签: c# asp.net-core ef-core-2.0 db-first