A
A
Arthur2017-07-29 16:27:38
ASP.NET
Arthur, 2017-07-29 16:27:38

How to make the TPT approach through the Fluent API when inheriting from a generic class?

Hello connoisseurs. I will describe the point. There are several service types (Service.cs, ServiceMeetGuest.cs) and ticket types (Bid.cs, BidMeetGuest.cs). With migrations in service types, everything is simple.
But the types of requests are inherited from the generalized class, the type of service is indicated in the generalized type TService. I tried to do it this way:
There is a generalized class:

public class BidBase<TService>
    {
        public long Id { get; set; }

        ...

        [ForeignKey("Service")]
        [Required]
        public long ServiceId { get; set; }
        public virtual TService Service { get; set; }
    }

And the declaration of the entity in the ApplicationDbContext:
public DbSet<BidBase<Service>> Bids { get; set; }
public DbSet<BidBase<ServiceMeetGuest>> BidsMeetGuest { get; set; }

As it turned out, Entity Framework does not support generic types.
So I decided to add classes:
public class Bid : BidBase<Service> { }
public class BidMeetGuest : BidBase<ServiceMeetGuest> {}

And DbSet's:
public DbSet<Bid> Bids { get; set; }
public DbSet<BidMeetGuest> BidsMeetGuest { get; set; }

Everything is OK, but there is no connection between them, the entity framework perceives them as 2 different entities, although they are inherited from BaseBid.cs. How to make a Table Per Type approach ( https://metanit.com/sharp/entityframework/7.2.php) in between via Fluent Api?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kuznetsov, 2017-07-29
@DarkRaven

You basically have THREE different types.
Since your BidBase does not participate as a separate DbSet (and, accordingly, there is no table for it), EF thinks that these are just two separate tables, the structure of which needs to be mapped one-to-one into separate tables

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question