Maintaining Complextype creation pattern in EF Core

Asked

Viewed 54 times

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);
        }

1 answer

0

Thus:

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 it was wrong, because it was to have created 2 tables too.

  • The problem is that I have several properties like this attributes, then I would have to inherit several classes....

  • no problem inherits from base class, the code is organized and easy to do manutecao, otherwise you will only generate more and more functions to do basic operations because your model you want does not fit in the EF

  • 1

    C# does not support multiple class inheritance, like the scenario @Brunohenri described. One possibility is the use of interfaces in the scenario, but still this does not prevent name conflict.

  • I don’t know how Voce arrived at this conclusion of confilite of names, but Voce can have ONE BASE CLASS and the rest interface, make a solution using EF only leaves the code slave EF @Onosendai

  • @Hudsonph Take a class, ClasseTeste, implementing two interfaces at the same time: IInterfaceA and IInterfaceB. Both specify a property with the same name, Valor. In C# you can use explicit implementations; EF Core (mentioned by AP) apparently has problems with this (https://github.com/aspnet/EntityFrameworkCore/issues/3790, https://readerman1.wordpress.com/2016/02/04/ef-context-explicit-interface-implementation-raises-an-exception/).

  • but I am not suggesting interface implementation but rather base class

  • The mention of the name conflict was related to the use of interfaces, which would be a viable implementation alternative compatible with the expectations of EF Core.

  • what you are saying is correct but does not apply in my solution, if there are 10 classes that need to have 2 equal fields (weight and value) the best solution is the creation of a baseclass for this problem

  • what is the advantage of having this baseclass, Voce does not need to write a function for each Voce Entity can do a single where T is its baseclass

Show 5 more comments

Browser other questions tagged

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