当我试图发布新的项目时,下面的事情就会发生。在本文中,应用程序使用中介器作为API。
的新对象
在邮递员中,有一个错误:
"errors": "An error occurred while updating the entries. See the inner exception for details."
当我检查终端时,会出现更具体的错误:
SQLite Error 19: 'UNIQUE constraint failed: PosterDto.Id'.
PosterDTO:
public class PosterDto
{
public string Id { get; set; }
public string DisplayName { get; set; }
public string Username { get; set; }
public string Photo { get; set; }
}
项目:
public class Item
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Image { get; set; }
public PosterDto Poster { get; set; }
public ICollection<Message> Messages { get; set; } = new List<Message>();
}
创建项目类:
public class Create
{
public class Command : IRequest
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Image { get; set; }
}
public class Validator : AbstractValidator<Command>
{
public Validator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Title).NotEmpty().WithMessage("Please specify a title");
RuleFor(x => x.Description).NotEmpty().WithMessage("Please specify a description");
RuleFor(x => x.Latitude).NotEmpty();
RuleFor(x => x.Longitude).NotEmpty();
}
}
public class Handler : IRequestHandler<Command>
{
private readonly DataContext _context;
private readonly UserManager<AppUser> _userManager;
private readonly IUserAccessor _userAccessor;
public Handler(DataContext context, UserManager<AppUser> userManager, IUserAccessor userAccessor)
{
_context = context;
_userManager = userManager;
_userAccessor = userAccessor;
}
public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
{
var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUsername());
if (user == null)
{
throw new RestException(HttpStatusCode.Unauthorized, new { user = "Not found" });
}
var poster = new PosterDto
{
Id = user.Id,
DisplayName = user.DisplayName,
Username = user.UserName,
Photo = user.Photo
};
var item = new Item
{
Id = request.Id,
Title = request.Title,
Description = request.Description,
Date = DateTime.Now,
Latitude = request.Latitude,
Longitude = request.Longitude,
Image = request.Image,
Poster = poster
};
_context.Items.Add(item);
var success = await _context.SaveChangesAsync() > 0;
if (success)
{
return Unit.Value;
}
throw new NotImplementedException("Error");
}
}
}
ItemsController:
[HttpPost]
public async Task<ActionResult<Unit>> Create(Create.Command command)
{
return await _mediatR.Send(command);
}
发布于 2021-07-30 16:48:46
您遇到的问题与实体框架有关。我想你是先使用代码了。
public class Item
{
// Entity Framework will create a relationship between PosterDto and Item
public PosterDto Poster { get; set; }
}
实体框架将在PosterDto和Item之间创建一个关系。要防止EntityFramework这样做,请将属性NotMapped添加到PosterDto属性中。
public class Item
{
// Add NotMapped Attribute
[NotMapped]
public PosterDto Poster { get; set; }
}
https://stackoverflow.com/questions/68593791
复制相似问题