Efcore: Multiple DbSets of the same entity

Created on 29 Sep 2016  路  1Comment  路  Source: dotnet/efcore

I'm working on a tool that is doing data migrations, and as part of that I need to store mappings of record ids in the old system to that in the new system. Since these mappings are essentially the same schema, I tried to model this as follows:

//DB Context
public DbSet<EntityMapping> UserMapping { get; set; }
public DbSet<EntityMapping> RoleMapping { get; set; }

//Common Entity
public class EntityMapping
{
    [Key]
    public int SourceId { get; set; }
    [Required]
    public int DestinationId { get; set; }
}

However all that just ended up with one table in the (sql) database called EntityMapping.

What is the recommended approach for modelling the same entity, in multiple tables?

>All comments

This isn't something we are planning to support in EF Core. Probably the best option is to use an un-mapped base type, and then have a concrete type for each table.

``` c#
//DB Context
public DbSet UserMapping { get; set; }
public DbSet RoleMapping { get; set; }

//Common Entity
public abstract class EntityMapping
{
[Key]
public int SourceId { get; set; }
[Required]
public int DestinationId { get; set; }
}

public class UserMapping : EntityMapping
{
}

public class RoleMapping: EntityMapping
{
}
```

Was this page helpful?
0 / 5 - 0 ratings