Plan properties in a single table

Asked

Viewed 45 times

1

Consider the following classes:

public class Pessoa
{
   public int Id { get; set; }
   public string Nome { get; set; }
   public Telefone Telefone { get; set; }
}

public class Telefone
{
   public string Numero { get; set; }
   public string Descricao { get; set; }
   public Operadora Operadora { get; set; }
}

public class Operadora
{
   public string Descricao { get; set; }
   public string Codigo { get; set; }
}

In the database (SQL Server) I want to have the following table:

+-------------------------------------------------------------------------+
| PessoaDados                                                             |
+----+------+--------+-----------+---------------------+------------------+
| Id | Nome | Numero | Descricao | Operadora_Descricao | Operadora_Codigo |
+----+------+--------+-----------+---------------------+------------------+

I’m using Entity Framework 6 Migrations, but when running the update command, Entity complains that the Phone class does not have Id.

I searched for Dataannotations, but I couldn’t find anything that would help.

Using Codefirst, it is possible to plan tables the way I am trying?

  • version is 6 or Core?

  • @Virgilionovic updated the question with the version. It would be the same EF 6.

  • And then friend tested the answer???

2 answers

0

Yes it is possible, you have to map the fields all in the same class configuration Pessoa, but, this has to be done by the configuration class and configuring the method OnModelCreating as follows:

Classes:

public class Pessoa
{
    public Pessoa()
    {
        Telefone = new Telefone();
    }
    public int Id { get; set; }
    public string Nome { get; set; }          
    public Telefone Telefone { get; set; }
}

public class Telefone
{
    public Telefone()
    {
        Operadora = new Operadora();
    }
    public string Numero { get; set; }
    public string Descricao { get; set; }            
    public Operadora Operadora { get; set; }
}

public class Operadora
{   
    public string Descricao { get; set; }
    public string Codigo { get; set; }        
}

Observing: in class Pessoa and Telefone its members that are classes are instantiated in the constructor so that no exception occurs in upgrades.

public class DatabaseContext: DbContext
{
    public const string ConnectionString = " *** ";

    public DbSet<Pessoa> Pessoas { get; set; }

    public DatabaseContext():
        base(ConnectionString)
    {            
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Pessoa>()
                .HasKey(x => x.Id)
                .Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<Pessoa>()
                .Property(x => x.Nome)
                .HasMaxLength(100)
                .IsRequired();

        modelBuilder.Entity<Pessoa>()
                .Property(x => x.Telefone.Descricao)
                .HasColumnName("Descricao")
                .HasMaxLength(100);

        modelBuilder.Entity<Pessoa>()
                .Property(x => x.Telefone.Numero)
                .HasColumnName("Numero")
                .HasMaxLength(100);

        modelBuilder.Entity<Pessoa>()
                .Property(x => x.Telefone.Operadora.Descricao)
                .HasColumnName("Operadora_Descricao")
                .HasMaxLength(100);

        modelBuilder.Entity<Pessoa>()
                .Property(x => x.Telefone.Operadora.Codigo)
                .HasColumnName("Operadora_Codigo")
                .HasMaxLength(100);
    }
}

In the above case everything has been set to Entity Pessoa and in each property create the settings as requested in the question.

-1

At this point I can see 3 observations:

  1. The way the class is declared, 01 person will have only 01 phone, when in fact, I imagine it should be several. For this case, in person the correct thing would be for you to have one List<Telefone>, with its proper initialization in the constructor.
  2. You will need the id in Phone and Operator, because the code first (by my simple knowledge) will create three tables, all properly related.
  3. As three tables would be created, an output for you would be to create a view in the way you expect, but in the case of EF, when a search by ID is requested, the whole object will be loaded, with the phones and the Operator, but for this, it will be necessary to configure the lazyloading.
  • thanks for the answer, but in this example I am not taking into account the modeling, only if the Entity Framework has or does not have the resource to plan more than one class in a single table.

Browser other questions tagged

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