如何将继承鉴别器映射为实体框架中的复合键?

Mat*_*nce 7 entity-framework

是否可以使用父键和鉴别器值映射一对一的关系?我知道代码首先不喜欢具体类上的discriminator属性,只能在Map方法中引用它.

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));
Run Code Online (Sandbox Code Playgroud)

/ *这是当前生成的* /

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 )
)
Run Code Online (Sandbox Code Playgroud)

/ *是否可以生成/映射并保持继承?* /

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 )
)
Run Code Online (Sandbox Code Playgroud)

谢谢.

Lad*_*nka 3

据我所知,这是不可能的,因为 EF 不允许在任何其他映射中使用鉴别器列。此外,您的目标映射将要求您的事务类也具有FlightTypeId属性(类必须具有整个键的属性),但这会破坏继承的含义,因为您将能够更改该属性的值并使您的继承不一致。