新建一个.NET Core类库项目,项目名称Repositories


然后为Repositories添加项目引用

添加CountriesRepository.cs实现ICountriesRepository接口
using Entities;
using Microsoft.EntityFrameworkCore;
using RepositoryContracts;
namespace Repositories
{
public class CountriesRepository : ICountriesRepository
{
private readonly ApplicationDbContext _db;
public CountriesRepository(ApplicationDbContext db)
{
_db = db;
}
public async Task<Country?> AddCountryAsync(Country country)
{
await _db.Countries.AddAsync(country);
await _db.SaveChangesAsync();
return country;
}
public async Task<IEnumerable<Country>?> GetAllCountriesAsync()
{
return await _db.Countries.ToListAsync();
}
public async Task<Country?> GetCountryByCountryIdAsync(Guid countryId)
{
return await _db.Countries.FirstOrDefaultAsync(c => c.CountryId == countryId);
}
public async Task<Country?> GetCountryByCountryNameAsync(string countryName)
{
return await _db.Countries.FirstOrDefaultAsync(c => c.CountryName == countryName);
}
}
}
Gitee获取源码:
145

被折叠的 条评论
为什么被折叠?



