Class relationship - (C# Entityframework)

Asked

Viewed 426 times

4

Hello,

I have a Person Class and an Address Class

Ex.

public class Pessoa
    {

        public int Id { get; set; }
        public string Nome { get; set; }
    ...
        public Endereco Endereco { get; set; }

    }

public class Endereco
    {
        public string Logradouro { get; set; }
        public string Numero { get; set; }
...
    }

When I run the Entityframework Update-Database an Address table with One to One relationship is generated, but the idea was that the address fields were part of the Person table without relationship, this is possible?

Ex. I would like the person table to have the fields Id, Nome, Logradouro, Numero.

It is possible to do this?

  • You generated DbSet for Endereco?

  • No, so I thought it wouldn’t generate the table..

  • 1

    I did some research and from what it seemed to me is not possible to popular the models the way you want. But one question, why do you need it to be done that way?

  • @Zignd is possible yes, take a look at my answer.

2 answers

1


Probably the EF is inferring that there is some PK, so it does not keep the convention for Complextypes, then how did not post the complete code of the class Endereço can’t tell which property is causing this, try to put the explicit setting to Complextype of Endereco, overrides the method OnModelCreating of DbContext in its context so:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.ComplexType<Endereco>();
}

Here’s a link about Complextypes, look at the section "How to implement a Complex Type with Code First" he uses exactly the example address, note that he starts the article by presenting a 1:1 relationship and then denormalizing and usefulness Complextypes.

0

If relationship is a person has an address gets like this:

 public class Pessoa
    {

        public int Id { get; set; }
        public string Nome { get; set; }
    ...
        public virtual Endereco Endereco { get; set; }

    }

    public class Endereco
        {
            public string Logradouro { get; set; }
            public string Numero { get; set; }
            public virtual Pessoa Pessoa { get; set; }

        }

If relationship is a person has many addresses will have to stay like this :

public class Pessoa
        {

            public int Id { get; set; }
            public string Nome { get; set; }

            public virtual List<Endereco> Endereco { get; set; }

        }

 public class Endereco
{

               public string Logradouro { get; set; }
                public string Numero { get; set; }
                public int IdPessoa //Chave estrangeira
                public virtual Pessoa Pessoa { get; set; }

}

this goes from the concept of data modeling 1:N primary key on the side a foreign key turns on the many side.still missing thing can take a look at entity mapping that you can do via datanotation or by Fluent API recommend taking a look at this site http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx

Browser other questions tagged

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