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

时间:2012-04-06 00:03:35

标签: 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));

/ *这是当前生成的内容* /

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 )
)

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

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 )
)

感谢。

1 个答案:

答案 0 :(得分:3)

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