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

Grouping by an alias

sql sql-server database database-design

SELECT COALESCE (rsu.last_name + ', ' + rsu.first_name + ' ' + rsu.middle_name + '.', rsu.last_name + ', ' + rsu.first_name) as student_name, rsu.day_id

Conditional select query

sql-server conditional union

I have a large SQL query with multiple statements and UNION ALL. I am doing something like this now:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.