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 w开发者_JAVA百科ith 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?
Try specifying the explicit mapping for this property using either the StoreName DataAnnotation attribute or the Fluent mapping HasColumnName method.
精彩评论