我正在尝试在EF中进行简单的多对一关系,并将其定义为:
type Car() =
[<DefaultValue>] val mutable private id : int
member x.ID with get() = x.id and set(v) = x.id <- v
[<DefaultValue>] val mutable private carName : string
member x.CarName with get() = x.carName and set(v) = x.carName <- v
[<DefaultValue>] val mutable private dealer : Dealer
member x.Dealer with get() = x.dealer and set(v) = x.dealer <- v
and Dealer() =
[<DefaultValue>] val mutable private id : int
member x.ID with get() = x.id and set(v) = x.id <- v
[<DefaultValue>] val mutable private name : string
member x.Name with get() = x.name and set(v) = x.name <- v
[<DefaultValue>] val mutable private cars : seq<Car>
member x.Cars with get() = x.cars and set(v) = x.cars <- v
type MyContext() =
inherit DbContext("MyContext")
[<DefaultValue(true)>] val mutable private cars : DbSet<Car>
member x.Cars with get() = x.cars and set(v) = x.cars <- v
[<DefaultValue(true)>] val mutable private dealers : DbSet<Dealer>
member x.Dealers with get() = x.dealers and set(v) = x.dealers <- v
并在控制台启动时以下列方式调用它:
let ctx = new MyContext()
Query.query <@ seq { for x in ctx.Cars do if x.CarName = "Volvo" then yield x.Dealer.Name } @>
以上将生成以下SQL查询:
SELECT
[Extent2].[Name] AS [Name]
FROM [dbo].[Cars] AS [Extent1]
LEFT OUTER JOIN [dbo].[Dealers] AS [Extent2] ON [Extent1].[Dealer_ID] = [Extent2].[ID]
WHERE N'Volvo' = [Extent1].[CarName]
现在是我的问题,为什么要在Car类型中定义经销商并将其转换为Dealer_ID
而不是DealerID?
答案 0 :(得分:1)