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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With