How to map inheritance discriminator as composite key in Entity Framework?

Is it possible to map a one to one relationship using the parent key and a discriminator value? I know that code first does not like the discriminator property on the concrete class and to reference it only in 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 generated */

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 generate/map this and keep 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.

Answers


As I know it is not possible because EF doesn't allow using discriminator column in any other mapping. Also your target mapping will demand that your transaction class also has FlightTypeId property (class must have properties for the whole key) but it would break the meaning of the inheritance because you would be able to change the value of that property and make your inheritance inconsistent.


Need Your Help

SQL Server: How do I maintain data integrity using aggregate functions with group by?

sql sql-server sql-server-2005

Here's my question: how do I maintain record integrity using aggregate functions with a group by?

Regular expressions but for writing in the match

python regex

When using regular expressions we generally, if not always use them to extract some kind of information. What I need is to replace the match value with some other value...