Maintaining ComplexType creation pattern in EF Core

0

Previously I used the Entity Framework 6.0, and when creating a class as in the example below:

public class Produto {
    public int ProdutoId { get; set; }
    public string NomeProduto { get; set; }
    public atributo atributo { get; set; }
}

public class atributo {
    public string peso { get; set; }
    public decimal valor { get; set; }
}

My DB was only created one table, Products:

with the columns

  • ProductId
  • ProductName
  • attribute_weight
  • attribute_value

However, when using EF Core, it creates two tables, Products and Attributes, and links the attribute with an FK in the Products table. I understand this behavior, however, I would like to stick to the old standard, as it was in EF 6.0.

Does anyone know how I can configure this?

EDIT

As said by our colleague @HudsonPH could inherit attributes in Products. But let's say I have multiple classes to use as properties in Products , would I have to inherit multiple classes? Another thing my idea is that when I'm programming other parts of the system, I use product.attribute.weight and not product.weight . I made a simple example in the question, but my real models are much more complex

EDIT 2

I found the solution , just add the following method its class context

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {   
            modelBuilder.Entity<Produto>()
                .OwnsOne<atributo>(s => s.atributo);

            base.OnModelCreating(modelBuilder);
        }
    
asked by anonymous 06.12.2017 / 03:23

1 answer

0

This way:

public class Produto : atributo {
    public int ProdutoId { get; set; }
    public string NomeProduto { get; set; }
}

public class atributo {
    public string peso { get; set; }
    public decimal valor { get; set; }
}

But the first way you did was wrong, because it was to have created 2 tables as well.

    
06.12.2017 / 09:48