我正在使用asp.netcore 6 web API、实体框架代码优先和CQRS - Mediatr用于一个项目,该项目具有更新数据库和从数据库获取数据并在swaggerui中显示数据的功能,但当我尝试通过数据库中的ID创建更新数据的处理程序时,我遇到了一个问题,我收到了这个错误:Error CS0311 The type 'EnocaChallengeV2.Commands.UpdateFirmDateCommand' cannot be used as type parameter 'TRequest' in the generic type or method 'IRequestHandler<TRequest, TResponse>'. There is no implicit reference conversion from 'EnocaChallengeV2.Commands.UpdateFirmDateCommand' to 'MediatR.IRequest<int>'.
这是我的命令
using EnocaChallengeV2.Models;
using MediatR;
namespace EnocaChallengeV2.Commands
{
public class UpdateFirmDateCommand : IRequest<Firm> {
public int Id { get; set; }
public DateTime startTime { get; set; }
public DateTime endTime { get; set; }
public UpdateFirmDateCommand(int id, DateTime StartTime, DateTime EndTime)
{
Id = id;
startTime = StartTime;
endTime = EndTime;
}
}
}
这是处理程序:
using EnocaChallengeV2.Commands;
using EnocaChallengeV2.Models;
using EnocaChallengeV2.Queries;
using EnocaChallengeV2.Repositories;
using MediatR;
namespace EnocaChallengeV2.Handlers
{
public class UpdateFirmDateHandler : IRequestHandler<UpdateFirmDateCommand, int>
{
private readonly IFirmRepository _firmRepository;
public UpdateFirmDateHandler(IFirmRepository firmRepository)
{
_firmRepository = firmRepository;
}
public async Task<int> Handle(UpdateFirmDateCommand command, CancellationToken cancellationToken)
{
var firm = await _firmRepository.GetFirmByIdAsync(command.Id);
if (firm == null)
return default;
firm.startTime = command.startTime;
firm.endTime = command.endTime;
return await _firmRepository.UpdateFirmDateAsync(firm);
}
}
}
这是一个接口:
using EnocaChallengeV2.Models;
namespace EnocaChallengeV2.Repositories
{
public interface IFirmRepository
{
public Task<List<Firm>> GetFirmListAsync();
public Task<Firm> GetFirmByIdAsync(int Id);
public Task<Firm> AddFirmAsync(Firm firm);
public Task<int> UpdateFirmDateAsync(Firm firm);
public Task<int> UpdateFirmVerificationAsync(Firm firm);
}
}
这是存储库:
using EnocaChallengeV2.Data;
using EnocaChallengeV2.Models;
using Microsoft.EntityFrameworkCore;
namespace EnocaChallengeV2.Repositories
{
public class FirmRepository : IFirmRepository
{
private readonly DbContextClass _dbContext;
public FirmRepository(DbContextClass dbContext)
{
_dbContext = dbContext;
}
public async Task<Firm> AddFirmAsync(Firm firm)
{
var result = _dbContext.Firms.Add(firm);
await _dbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<int> DeleteFirmAsync(int Id)
{
var filteredData = _dbContext.Firms.Where(x => x.Id == Id).FirstOrDefault();
_dbContext.Firms.Remove(filteredData);
return await _dbContext.SaveChangesAsync();
}
public async Task<Firm> GetFirmByIdAsync(int Id)
{
return await _dbContext.Firms.Where(x => x.Id == Id).FirstOrDefaultAsync();
}
public async Task<List<Firm>> GetFirmListAsync()
{
return await _dbContext.Firms.ToListAsync();
}
public async Task<int> UpdateFirmDateByIdAsync(Firm firm)
{
_dbContext.Firms.Update(firm);
return await _dbContext.SaveChangesAsync();
}
//public async Task<Firm> UpdateFirmVerificationAsync(Firm firm)
//{
// _dbContext.Firms.Update(firm);
// return await _dbContext.SaveChangesAsync();
//}
}
}
我尝试将Task〈〉更改为Firm,然后更改为int,但似乎不起作用。
1条答案
按热度按时间eivgtgni1#
您的命令需要定义返回类型“int”:
注意更改: