EF Core - How to map a field from a table that relates to two fields from another table

Asked

Viewed 177 times

0

I have a Person class (Personal Camp) that relates to the Personal class Accounting (Fields: Personal Accounting, Bookkeeping, Personal and Personal).

Rationale: In the Person table the natural and legal persons will be stored with different Ids:

Ex:

1 - Peter*

2 - Mary*

3 - Banco Santander

4 - Banco do Brasil

5 - Sicoob

Let’s say, in the Personal table, I need to keep an account of Maria whose bank is Sicoo... then, the Personal field of the Person table should be related to the Personal and Personal fields of the Personal table...

Ex:

Personal accounting = 1

Pessoaid = 2 (Maria)

Banbancoid = 5 (Sicoob)

How do I do this using Entity Framework Core? Follow my current code:

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

        public virtual PessoaTipo PessoaTipo { get; set; }        
        public virtual ICollection<PessoaContaBancaria> PessoaContasBancarias { get; set; }
    }

public class PessoaContaBancaria
    {
        public int PessoaContaBancariaId { get; set; }
        public int PessoaId { get; set; }    
        public int PessoaBancoId { get; set; }
        public string Agencia { get; set; }
        public string NumeroConta { get; set; }
        public DateTime DataInclusao { get; set; }
        public bool Sistema { get; set; }

        public virtual Pessoa Pessoa { get; set; }
    }

public void PessoaContaBancariaMapping(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PessoaContaBancaria>()
                .ToTable("tblPessoaContaBancaria");

            modelBuilder.Entity<PessoaContaBancaria>()
                .HasKey(p => new { p.PessoaContaBancariaId, p.PessoaId });

            modelBuilder.Entity<PessoaContaBancaria>()
                .HasOne(p => p.Pessoa)
                .WithMany(p => p.PessoaContasBancarias)
                .HasForeignKey(p => p.PessoaId)
                .IsRequired();

            modelBuilder.Entity<PessoaContaBancaria>()
               .Property(p => p.Agencia)
               .HasColumnName("Agencia")
               .HasColumnType("Varchar(15)")
               .HasMaxLength(15)
               .IsRequired();

            modelBuilder.Entity<PessoaContaBancaria>()
               .Property(p => p.NumeroConta)
               .HasColumnName("NumeroConta")
               .HasColumnType("Varchar(15)")
               .HasMaxLength(15)
               .IsRequired();

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

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

  • Let me understand, you want to reference two values of a single entity/class or each value a different entity/class?

  • It would be "each value a different entity/class" as One value would be for the person and another for the person who is defined as Bank.

  • 1

    So you want to reference two id’s of the same class(person), which represents both a physical and legal person. I would separate this into two entities and inherit from a common person class. But try to reference two id’s in the mapping. First in the Personal Entity Accounting, puts another attribute of the type person: "Personal Personbank". Since you want to relate two people in the same entity. Finally refer another person’s foreign key in the mapping, representing Peopleanbank that we have just used. I didn’t test it because I’m not in the mood here, but try and tell me.

  • In vdd is I have two Personal and Physical classes that are related to the Person. They make this Classification... I followed your suggestion: I created a PERSONAL property in the Person class and peed like this: modelBuilder.Entity<Personal . Hasone(p => p.Personal) . Withmany(p => p.Personal Accountingbank) . Hasforeignkey(p => p.Pessoabancoid) . Isrequired();

  • Unfortunately in powershel in Package Manager Console it shows the error: Cannot create a Relationship between 'Person.Personal Accountingbank and 'Personal.Personal because there is already a Relationship between 'Person.Personal Accountingbanks' and 'Personal Accountingbanks.Person'. Navigation properties can only participate in a single Relationship.

  • I believe he is making confusion with the first relationship using the navigation property PERSON who will make an equal relationship but to map the Person....

  • Just to correct: I created a PERSONAL property in the Personal Classbank and not in the Person class as I said in the first comment.

  • Updates the question with the current state, even with the classes. Note: Why Peopleworkery have many people, it has to be referenced as collection in the Personal class Bankbanking: Icollection<Person> Personal Physique and Icollection<Person> Personal.

Show 3 more comments
No answers

Browser other questions tagged

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