Efcore: Force class to ignore table per hierarchy and map separately?

Created on 16 Apr 2018  路  1Comment  路  Source: dotnet/efcore

Howdy!

I have 3 classes, each of them deriving from Person. I would like only 2 of these classes to be in the same table Person and 3rd class to be mapped separately (go to the other table).

Is this possible to somehow force this kind of behaviour? I tried to add [Table("tblname")] attribute above the class I want to map separately but it didn't do the trick.

Is this even possible?

public class Person
{
    public int Id { get; set; }
    public string Something { get; set; }
}

public class Person1 : Person
{
    public int Prop1 { get; set; }
}

public class Person2 : Person
{
    public int Prop2 { get; set; }
}

// Make it go to separate table?
[Table("People3")]
public class Person3 : Person
{
    public int Prop3 { get; set; }
}

// my db sets
public DbSet<Person> People { get; set; }
public DbSet<Person1> People1 { get; set; }
public DbSet<Person2> People2 { get; set; }
// go to separate table?
public DbSet<Person3> People3 { get; set; }

I also tried doing it the fluent way but it didn't work:

modelBuilder.Entity<Person3>().ToTable("People3");
closed-question

Most helpful comment

@enemyofthedawn All three types mapping to their own table if the Person base class it not mapped--for example, buy removing the DbSet for it in the code above. (This will only work if the base class does not have relationships defined on it.) Beyond this, two have Person1 and Person2 use the same table (TPH), but have Person3 use a different table would require a new base class between Person1 and Person2. Something like:
```C#
public class Person
{
public int Id { get; set; }
public string Something { get; set; }
}

public class MappedPerson : Person
{
}

public class Person1 : MappedPerson
{
public int Prop1 { get; set; }
}

public class Person2 : MappedPerson
{
public int Prop2 { get; set; }
}

public class Person3 : Person
{
public int Prop3 { get; set; }
}

// my db sets
public DbSet People { get; set; }
public DbSet People1 { get; set; }
public DbSet People2 { get; set; }
public DbSet People3 { get; set; }
```

>All comments

@enemyofthedawn All three types mapping to their own table if the Person base class it not mapped--for example, buy removing the DbSet for it in the code above. (This will only work if the base class does not have relationships defined on it.) Beyond this, two have Person1 and Person2 use the same table (TPH), but have Person3 use a different table would require a new base class between Person1 and Person2. Something like:
```C#
public class Person
{
public int Id { get; set; }
public string Something { get; set; }
}

public class MappedPerson : Person
{
}

public class Person1 : MappedPerson
{
public int Prop1 { get; set; }
}

public class Person2 : MappedPerson
{
public int Prop2 { get; set; }
}

public class Person3 : Person
{
public int Prop3 { get; set; }
}

// my db sets
public DbSet People { get; set; }
public DbSet People1 { get; set; }
public DbSet People2 { get; set; }
public DbSet People3 { get; set; }
```

Was this page helpful?
0 / 5 - 0 ratings