How to match inheritance discriminator as a composite key in Entity Framework?

Is it possible to match the one-to-one relationship using the parent key and the discriminator value? I know that this code at first does not like the discriminator property in a particular class and refers only to the Map method.

FlightTypes { Inbound = 1, Outbound = 2} public class Transaction - int TransactionId - int? InboundFlightId - InboundTransactionFlight InboundFlight - int? OutboundFlightId - OutboundTransactionFlight OutboundFlight public abstract class TransactionFlight - TransactionFlightId public class InboundTransactionFlight : Flight - List<Transaction> InboundFor public class OutboundTransactionFlight : Flight - List<Transaction> OutboundFor Entity<InboundTransactionFlight>().Map(m => m.Requires("FlightTypeId").HasValue(1)); Entity<OutboundTransactionFlight>().Map(m => m.Requires("FlightTypeId").HasValue(2)); 

/ * this is what is currently created * /

 CREATE TABLE Transactions ( TransactionId int NOT NULL, InboundFlightId int NULL, OutboundFlightId int NULL ) CREATE TABLE TransactionFlights ( TransactionFlightId int NOT NULL, FlightTypeId int NOT NULL, ... CONSTRAINT PK_TransactionFlights PRIMARY KEY CLUSTERED ( TransactionFlightId ) ) 

/ * is it possible to create / display this and keep the inheritance? * /

 CREATE TABLE Transactions ( TransactionId int NOT NULL, ) CREATE TABLE TransactionFlights ( TransactionId int NOT NULL, FlightTypeId int NOT NULL, ... CONSTRAINT PK_TransactionFlights PRIMARY KEY CLUSTERED ( TransactionId, FlightTypeId ) ) 

Thanks.

+6
source share
1 answer

As I know, this is not possible because EF does not allow the discriminator column to be used in any other mapping. In addition, your target mapping will require your transaction class to have the FlightTypeId property (the class must have properties for the entire key), but it would violate the meaning of inheritance, because you could change the value of this property and make your Inheritance inconsistent.

+3
source

Source: https://habr.com/ru/post/912550/


All Articles