PK and FK Conflict in SQL Server Database

0

I am facing a Primary Key and Foreign Key conflict problem in SQL Server 2017 Express. I am using ASP.NET with Entity Framework Code First (Migrations). The problem is that when I add a new Client that is a Person does not add. The Entity Framework even creates the tables with the relationship, places the primary key in both tables, also places the ClientId as FK in the Customer table, but does not enter the database.

Error occurred:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "Person_FK_FK". The conflict occurred in the database "TestTecnicoDB", table "dbo.Pessoa", column 'PessoaId'. The statement has been terminated.

Client Class:

public class Cliente
{
    [Key]
    [ForeignKey("Pessoa")]
    public int ClienteId { get; set; }
    public virtual Pessoa Pessoa { get; set; }

    public Cliente()
    {
        Pessoa pessoa = new Pessoa();
        ClienteId = pessoa.PessoaId;
    }
}

Class Client Configuration

public class ClienteConfiguration : EntityTypeConfiguration<Cliente>
{
    public ClienteConfiguration()
    {
        HasKey(c => c.ClienteId);
    }
}

Person Class

public class Pessoa
{
    public int PessoaId { get; set; }
    public TipoPessoa TipoPessoa { get; set; }

    public virtual Cliente Cliente { get; set; }
    public virtual PessoaFisica PessoaFisica { get; set; }
    public virtual PessoaJuridica PessoaJuridica { get; set; }
    public IEnumerable<Endereco> Enderecos { get; set; }
}

Person Class Configuration

public class PessoaConfiguration : EntityTypeConfiguration<Pessoa>
{
    public PessoaConfiguration()
    {
        HasKey(p => p.PessoaId);
    }
}

Tables in the Bank:

Does anyone know how this issue can be resolved?

    
asked by anonymous 29.10.2018 / 00:36

0 answers