6
是否可以使用父鍵和鑑別符值映射一對一關係?我知道代碼首先不喜歡具體類的鑑別器屬性,並且只能在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)
)
謝謝。
這是一個非常好的點拉迪斯拉夫,當然回答了我的問題。 EF確實對我的數據庫設計習慣提出了挑戰。我相信更好。 – 2012-04-07 01:38:22
雖然他們可以允許只讀... – VdesmedT 2016-05-04 13:26:41