Many-To-Many Relationship Mapping - EF Core - C#

Asked

Viewed 1,394 times

4

I’ve tried mapping a three-class relationship, but I’m getting it. I have a class Person who represents any Person, an Employee, a Customer, a Supplier, in short, any type of Person, be it physical or legal... When I register a certain type of person, I will need to use the Personal Registration table in order to save more specific data of each person... Note that many fields relate to the Personal field of tblPessoa (except the Id field, because it will only be a PK field and does not relate to any field), but I’m not getting the foreign keys right... The result I got was this:

Resultado que obtive

What I need to change in my classes and in mapping?

In the database I need the tables to look like this:

Preciso que as tabelas fiquem assim

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

        public virtual PessoaTipo PessoaTipo { get; set; }
        public virtual PessoaFisica PessoaFisica { get; set; }
        public virtual PessoaJuridica PessoaJuridica { get; set; }
        public virtual ICollection<PessoaCadastro> PessoasCadastros { get; set; }

    }

public class PessoaCadTipo
    {
        public int PessoaCadTipoId { get; set; }
        public string Descricao { get; set; }
        public bool Sistema { get; set; }
        public DateTime DataInclusao { get; set; }

        public virtual ICollection<PessoaCadastro> PessoasCadastros { get; set; }
    }

public class PessoaCadastro
    {
        public int PessoaId { get; set; }
        public int PessoaCadTipoId { get; set; }
        public int Id { get; set; }
        public int PessoaFilialId { get; set; }
        public int PessoaFilialCadId { get; set; }
        public int PessoaFuncCadId { get; set; }
        public DateTime DataInclusao { get; set; }
        public bool Sistema { get; set; }

        public virtual Pessoa Pessoa { get; set; }
        public virtual PessoaCadTipo PessoaCadTipo { get; set; }

    }

//Mapeamento da Classe PessoaCadastro
public void PessoaCadastroMapping(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PessoaCadastro>()
                .ToTable("tblPessoaCadastro");

            modelBuilder.Entity<PessoaCadastro>()
                .HasKey(p => new {p.PessoaId, p.PessoaCadTipoId, p.Id, p.PessoaFilialId });

            modelBuilder.Entity<PessoaCadastro>()
                .HasOne(p => p.Pessoa)
                .WithMany(p => p.PessoasCadastros)
                .HasForeignKey(p => p.PessoaId)
                .HasConstraintName("FK_Pessoa_PessoaCad")
                .IsRequired();

            modelBuilder.Entity<PessoaCadastro>()
                .HasOne(p => p.PessoaCadTipo)
                .WithMany(p => p.PessoasCadastros)
                .HasForeignKey(p => p.PessoaCadTipoId)
                .HasConstraintName("FK_PessoaCadTipo_PessoaCad")
                .IsRequired();

            modelBuilder.Entity<PessoaCadastro>()
                .HasOne(p => p.Pessoa)
                .WithMany(p => p.PessoasCadastros)
                .HasForeignKey(p => p.PessoaFilialId)
                .HasConstraintName("FK_PessoaFilial_PessoaCad")
                .IsRequired();

            modelBuilder.Entity<PessoaCadastro>()
                .HasOne(p => p.Pessoa)
                .WithMany(p => p.PessoasCadastros)
                .HasForeignKey(p => p.PessoaFilialCadId)
                .HasConstraintName("FK_PessoaFilialCad_PessoaCad")
                .IsRequired();

            modelBuilder.Entity<PessoaCadastro>()
                .HasOne(p => p.Pessoa)
                .WithMany(p => p.PessoasCadastros)
                .HasForeignKey(p => p.PessoaFuncCadId)
                .HasConstraintName("FK_PessoaFuncCad_PessoaCad")
                .IsRequired();

            modelBuilder.Entity<PessoaCadastro>()
                .Property(p => p.Sistema)
                .HasColumnName("Sistema")
                .HasColumnType("Bit");

            modelBuilder.Entity<PessoaCadastro>()
                .Property(p => p.DataInclusao)
                .HasColumnName("DataInclusao")
                .HasColumnType("DateTime")
                .IsRequired();
        }

1 answer

5


No need to complicate so much. Let’s first look at your case:

I have a class Person who represents any Person, an Employee, a Customer, a Supplier, in short, any type of Person, whether physical or legal.

In my view, a person can only be either physical or legal, but it can also be a customer, a supplier, both or no.

That is to say, Pessoa, PessoaFisica and PessoaJuridica are cases of inheritance, as I explain here. Cliente and Fornecedor are cases of composition, as I explain here.

When I register a certain type of person, I will need to use the Personal Registration table to save more specific data of each person.

No need. See the answers.

What I need to change in my classes and in mapping?

Well, put the two answers together and you’ll get what you’re looking for. Get rid of the Fluent API too. You don’t need it.

  • I read your answers about inheritance and composition, very good. A question: There are problems in making only a register of people, either for physical or legal?

  • No problem. It depends on the goal. I have a course in which I discuss inheritance and composition strategies with the student, in addition to using unified table or not. I would like to know more?

  • 1

    I’m already aware of the courses you teach by Hangouts, but at the moment I can’t, I’m doing one of ASP.NET MVC, then I already have one of Entity. Thank you and congratulations on your answers! =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.