Entityframework.docs: Can we use an abstract class as a base class of the hierarchy?

Created on 19 Nov 2018  Â·  1Comment  Â·  Source: dotnet/EntityFramework.Docs

Can we use an abstract class as a base class of the hierarchy? Please, add example of the DB settings.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

closed-question

Most helpful comment

Yes, I've tried this in one of my recently EF Core project.
For the parent class and child classes, it will AS IS what we doing usually.
For example:
```C#
public abstract class Function { ...}
public class Menu : Function {...}

In the DBContext class, we can expose above classes to DBSet, and only need to define the Entity for `<Function>` :
```C#
public class MyDBContext : DbContent
{
        public virtual DbSet<Function> Functions { get; set; }
        public virtual DbSet<Menu> Menus { get; set; }
...
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
...
modelBuilder.Entity<Function>(entity =>
            {.....});
....
}

But if you want to have any other specific requirements such as Ignore Properties of sub class, you can continue declare these, for example:
C# modelBuilder.Entity<Memu>().Ignore(e => e.MenuProperty1);

>All comments

Yes, I've tried this in one of my recently EF Core project.
For the parent class and child classes, it will AS IS what we doing usually.
For example:
```C#
public abstract class Function { ...}
public class Menu : Function {...}

In the DBContext class, we can expose above classes to DBSet, and only need to define the Entity for `<Function>` :
```C#
public class MyDBContext : DbContent
{
        public virtual DbSet<Function> Functions { get; set; }
        public virtual DbSet<Menu> Menus { get; set; }
...
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
...
modelBuilder.Entity<Function>(entity =>
            {.....});
....
}

But if you want to have any other specific requirements such as Ignore Properties of sub class, you can continue declare these, for example:
C# modelBuilder.Entity<Memu>().Ignore(e => e.MenuProperty1);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidliang2008 picture davidliang2008  Â·  4Comments

jaxidian picture jaxidian  Â·  4Comments

norvegec picture norvegec  Â·  3Comments

ajcvickers picture ajcvickers  Â·  4Comments

divega picture divega  Â·  4Comments