Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 463
Mathew Vance Avatar asked Apr 06 '12 00:04

Mathew Vance


People also ask

How do I create a composite key in Entity Framework?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.

What is meant by composite primary key in Entity Framework Code First?

Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys. In order to use composite keys, Entity Framework requires you to define an order for the key properties. You can do this by using the Column annotation to specify an order.

How does entity Framework handle inheritance?

Inheritance in the Entity Framework is similar to inheritance for classes in C#. In Entity Framework, you can map an inheritance hierarchy to single or multiple database tables based on your requirements. In this article, you will learn how to map your inheritance hierarchy to database tables in SQL Server.

How do you configure a primary key for an entity in Entity Framework fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.


1 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.

like image 67
Ladislav Mrnka Avatar answered Oct 20 '22 16:10

Ladislav Mrnka