F#-> EF生成错误的sql

发布于 2024-10-24 03:31:53 字数 1738 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-10-31 03:31:53

Try specifying the explicit mapping for this property using either the StoreName DataAnnotation attribute or the Fluent mapping HasColumnName method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文