0
In the past I used 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 BD was only created one table, Products:
with the columns
- Productoid
- Product name
- heaviness
- value_attribute
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, but I would like to maintain the old standard, as it was in EF 6.0.
Does anyone know how I can set up this way?
EDIT
As our colleague @Hudsonph said, you could inherit attributes in Products. But let’s say I have several classes to use as properties in Products, I would have to inherit several classes? Another thing my idea is that when I go to program other parts of the system, I use, product.weightattribute. and not product weight.. I made a simple example in question, but my real models are much more complex
EDIT 2
I found the solution, just add the following method to your context class
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Produto>()
.OwnsOne<atributo>(s => s.atributo);
base.OnModelCreating(modelBuilder);
}
Possible duplicate of Map Object value to Entity Core
– novic