Efcore: Using the same class (model) to create two tables

Created on 22 Dec 2016  路  1Comment  路  Source: dotnet/efcore

@rowanmiller can you help-me ?

im using SqlLite and .net core. How can i reuse the same class (Model) to create two table?

 public class Venda
    {
        public int ID { get; set; }
        public int MasterID { get; set; }
        public int UsuarioID { get; set; }
        public int Status { get; set; }
    }

/* **********************************************  */

  public class MyEntities : DbContext
    {
        public DbSet<Venda> VendasRecebidas { get; set; } //Table onde
        public DbSet<Venda> VendasTemp { get; set; } //Table two

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Filename=./mydb.db");
        }
    }
closed-question

Most helpful comment

@LucasArgate EF Core only supports having one mapping in the model for each entity type. I.e. there aren't any supported cases in which two instance of the same type could end up being stored in different tables or different columns.

If you want something like this there are a couple of alternatives you can try. The most appropriate one is going to depend on your scenario:

  1. Having two DbSets of two different types. The two types can inherit from (and even have all their properties defined in) a common base type as long as that base type isn't mapped in the EF Core model.

  2. Having two separate derived DbContext types which map the Venda type to different tables.

>All comments

@LucasArgate EF Core only supports having one mapping in the model for each entity type. I.e. there aren't any supported cases in which two instance of the same type could end up being stored in different tables or different columns.

If you want something like this there are a couple of alternatives you can try. The most appropriate one is going to depend on your scenario:

  1. Having two DbSets of two different types. The two types can inherit from (and even have all their properties defined in) a common base type as long as that base type isn't mapped in the EF Core model.

  2. Having two separate derived DbContext types which map the Venda type to different tables.

Was this page helpful?
0 / 5 - 0 ratings