5

I have to extend current database entity (Foo) to include new fields. Sounds easy but I struggle with solution. Problem is, I'm not able to create a partial class for Foo because it's part of a dll which I'm using. And there is no way how I could request modify Foo (or do it by my own) as I wish.

My project structure is similar to this:

// unmodifiable because part of dll
class Foo 
{
    int Id { get; set; }
    string Name { get; set; }
}

// unmodifiable because part of dll
interface IFooContext 
{
     DbSet<Foo> Foo { get; set; }
}

class FooContext: DbContext, IFooContext 
{
     DbSet<Foo> Foo { get; set; }
}

With my limited knowledge of .net, what I tried, was create class Bar what extends Foo and expected it would be enough to implement IFooContext with extended class Bar.

Below you can see, but it will end up with error, because C# doesn't allow use extended class interchangeable for parent.

class Bar: Foo 
{
     string Description { get; set; } //new field
}

class FooLoader: DbContext, IFooContext 
{
     DbSet<Bar> Foo; //error because DbSet<Foo> required
}

What I could do, is create property DbSet<Bar> Bar { get; set; } inside FooLoader, but then I will end up with two almost identical database tables: Foo and Bar.

So I made modification and created class Bar which will contain FooId. It results into two tables, where Bar table has a reference to Foo.

class Bar 
{
     int Id { get; set; }

     Foo Foo { get; set; }
     int FooId { get; set; }

     string Description { get; set; }
}

Both cases, will create a new table what I'm not sure, if is what I looking for. But is there any other way how to do it and have new fields included in base class/table?

2
  • when you don't have access to dll, then why to extend it? you could create Bar model entirely (including the properties of Foo) in your context right?
    – sam
    Dec 20, 2019 at 14:12
  • Because its part of framework guts which depends on IFooLoader implementation and what I have to do, is provide implementation of it. But in same time I need new parameters for my use cases.
    – Tomas Ivan
    Dec 23, 2019 at 10:09

1 Answer 1

5

Code first approach gives you at least 3 options to map your (inheritance hierarchy) entities to tables:

  1. Table per Hierarchy (TPH)
  2. Table per Type (TPT)
  3. Table per Concrete Type (TPC)

What you need is the 1st approach, which will map your base class and hieratances to one table, but an additional column ("Discriminator" as default name) will be added to identify their Type, which column will be used to revert to its own type when you query the full row from database. and most important: the 1st approach also gives best performance, comparing the other 2 approachs.

here are good posts where you can find implementation:

  1. https://learn.microsoft.com/en-us/ef/core/modeling/relational/inheritance
  2. https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/inheritance?view=aspnetcore-3.1
  3. https://www.learnentityframeworkcore.com/inheritance/table-per-hierarchy

it is simply like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ....

    modelBuilder.Entity<Foo>()
            .HasDiscriminator<string>("FooType")
            .HasValue<Foo>(nameof(Foo))
            .HasValue<Bar>(nameof(Bar))
            .HasValue<Other>(nameof(Other));
    ...
    base.OnModelCreating(modelBuilder);
}

or

modelBuilder.Entity<Foo>()
        .HasDiscriminator<int>("FooType")
        .HasValue<Foo>(1)
        .HasValue<Bar>(2)
        .HasValue<Other>(3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.