Код: Выделить всё
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (14ms) [Parameters=[@p0='?' (DbType = Guid), @p1='?', @p2='?' (DbType = DateTime), @p3='?' (DbType = Boolean), @p4='?', @p5='?' (DbType = Object), @p6='?', @p7='?' (DbType = Guid)], CommandType='Text', CommandTimeout='30']
INSERT INTO "Posts" ("Id", "Content", "CreatedAt", "IsArchive", "Location", "Tags", "Title", "UserId")
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
fail: Microsoft.EntityFrameworkCore.Update[10000]
An exception occurred in the database while saving changes for context type 'Facebook.Infrastructure.Common.Persistence.FacebookDbContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "PK_Posts"
DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Facebook.Domain.User;
namespace Facebook.Domain.Post
{
public class PostEntity
{
public Guid Id { get; set; }
public string? Title { get; set; }
public string? Content { get; set; }
public List? Tags { get; set; }
public string? Location { get; set; }
public ICollection? Images { get; set; }
public bool IsArchive { get; set; }
private DateTime _createdAt;
public DateTime CreatedAt
{
get { return _createdAt; }
set { _createdAt = DateTime.SpecifyKind(value, DateTimeKind.Utc); }
}
[ForeignKey("UserEntity")]
public Guid UserId { get; set; }
public UserEntity User { get; set; }
}
}
Код: Выделить всё
using Facebook.Domain.Post;
using Facebook.Domain.Story;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
namespace Facebook.Domain.User
{
public class UserEntity : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
private DateTime _birthday;
public DateTime Birthday
{
get { return _birthday; }
set { _birthday = DateTime.SpecifyKind(value, DateTimeKind.Utc); }
}
public string? Avatar { get; set; }
public string Gender { get; set; }
public ICollection Stories { get; set; } = new List();
public ICollection
Posts { get; set; } = new List();
}
}
Убедился, что идентификатор для каждого нового PostEntity уникален, с помощью Guid.NewGuid().
Проверил базу данных, чтобы убедиться в отсутствии дубликатов. Значения идентификаторов существуют
Что может быть причиной того, что повторяющееся значение ключа нарушает уникальное ограничение "PK_Posts" и как я могу решить эту проблему? Будем признательны за любые рекомендации по дальнейшему устранению этой проблемы.
Подробнее здесь: https://stackoverflow.com/questions/785 ... -constrain