F#-> EF生成错误的sql
我正在尝试在 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 并将其转换为 Dealer_ID
而不是 DealerID?
I'm trying to do a simple many-to-one relationship in EF and have defined it like:
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
and calling it the following way in the console start:
let ctx = new MyContext()
Query.query <@ seq { for x in ctx.Cars do if x.CarName = "Volvo" then yield x.Dealer.Name } @>
the above will generate the following SQL Query:
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]
Now is my question, why do it take the Dealer defined in the Car type and translate it to Dealer_ID
instead of DealerID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 StoreName DataAnnotation 属性 或 流畅映射 HasColumnName 方法。
Try specifying the explicit mapping for this property using either the StoreName DataAnnotation attribute or the Fluent mapping HasColumnName method.