Relationship between classes - (C # EntityFramework)

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 a one-to-one relationship is generated, but the idea was that the address fields were part of the Non-relationship table, is it possible?

Ex. I would like the person table to have the fields Id, Name, Backyard, Number .

Is it possible to do this?

    
asked by anonymous 15.01.2016 / 21:50

2 answers

1

Probably the EF is inferring that there is some PK, so it does not keep the convention for ComplexTypes , so since it did not post the full code of class Endereço I do not know what property it is causing this tries to put the explicit configuration for ComplexType of Endereco , override the OnModelCreating method of DbContext in your context like this:

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

Follow a link to ComplexTypes , look at the "How to implement the Complex Type with Code First" section . address, note that it begins the article presenting a 1: 1 relationship and then denormalizes and uses ComplexTypes

    
16.01.2016 / 02:06
0

If relationship is a person has an address looks 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 of the side one turns foreign key of the many side.still missing thing can take a look at entity mapping that you can do via datanotation or by fluent API I recommend giving one look at this site link

    
16.01.2016 / 02:34