Entity Framework 兼容 POCO 的最短语法
Shortest syntax for Entity Framework compatible POCO
我需要 F# 才能无缝地使用 Entity Framework。我正在尝试从这里改编示例:https://blogs.msdn.microsoft.com/visualstudio/2011/04/04/f-code-first-development-with-entity-framework-4-1/
问题是那里的record declaration太可怕了,我简直无法接受
type public Car() =
let mutable m_ID : int = 0
let mutable m_name : string = ""
[<Key>]
member public this.ID with get() = m_ID
and set v = m_ID <- v
member public this.Name with get() = m_name
and set v = m_name <- v
我试过 CLIMutable 是这样的:
module Program
[<CLIMutable>]
type Car = {
Name:string
}
let c = new Car(Name = "Honda")
它导致错误:"No contructors are available for the type 'Car'"。
据我了解,这个答案可能是一种解释:
我也试过类似的东西:
[<CLIMutable>]
type Car =
{
ID:int
} member this.Name = ""
错误同上。我真的很失望。有人可以帮忙吗?
CLIMutable
属性对 F# 使用站点没有任何影响。添加的默认构造函数无法从 F# 用户代码访问,可变属性也是如此。如果您想使用 EF 更改跟踪(在 F# 中),记录不是一个好的选择(因为您不能更改它,除非您声明所有字段 mutable
)。
如果可能,考虑使用例如SQL Provider. On the other hand e.g. Dapper 支持 serializing/deserializing POCO,因此支持 F# 记录。
由于记录从 F# 的角度看是不可变的,因此适用常规构造:
let c = { Name = "Honda" }
或(消除歧义,如果您还有 type Person = { Name : string }
)
let c = { Car.Name = "Honda" }
let c : Car = { Name = "Honda" }
let c = { Name = "Honda" } : Car
这似乎对我有用。这不是记录,而是 class。也可能不能命名为POCO。
//file DataModel.fs
module DataModel
open System.ComponentModel.DataAnnotations
open System.Data.Entity
type Car()=
[<Key>]
member val Id = 0 with get,set
member val Name = "" with get,set
type public CLCars() =
inherit DbContext()
member val Cars: DbSet<Car> = base.Set<Car>() with get,set
//file Program.fs
module Program
open DataModel
let db = new CLCars()
let c = new Car(Name="Honda")
db.Cars.Add(c) |> ignore
db.SaveChanges() |> ignore
我需要 F# 才能无缝地使用 Entity Framework。我正在尝试从这里改编示例:https://blogs.msdn.microsoft.com/visualstudio/2011/04/04/f-code-first-development-with-entity-framework-4-1/ 问题是那里的record declaration太可怕了,我简直无法接受
type public Car() =
let mutable m_ID : int = 0
let mutable m_name : string = ""
[<Key>]
member public this.ID with get() = m_ID
and set v = m_ID <- v
member public this.Name with get() = m_name
and set v = m_name <- v
我试过 CLIMutable 是这样的:
module Program
[<CLIMutable>]
type Car = {
Name:string
}
let c = new Car(Name = "Honda")
它导致错误:"No contructors are available for the type 'Car'"。 据我了解,这个答案可能是一种解释:
我也试过类似的东西:
[<CLIMutable>]
type Car =
{
ID:int
} member this.Name = ""
错误同上。我真的很失望。有人可以帮忙吗?
CLIMutable
属性对 F# 使用站点没有任何影响。添加的默认构造函数无法从 F# 用户代码访问,可变属性也是如此。如果您想使用 EF 更改跟踪(在 F# 中),记录不是一个好的选择(因为您不能更改它,除非您声明所有字段 mutable
)。
如果可能,考虑使用例如SQL Provider. On the other hand e.g. Dapper 支持 serializing/deserializing POCO,因此支持 F# 记录。
由于记录从 F# 的角度看是不可变的,因此适用常规构造:
let c = { Name = "Honda" }
或(消除歧义,如果您还有 type Person = { Name : string }
)
let c = { Car.Name = "Honda" }
let c : Car = { Name = "Honda" }
let c = { Name = "Honda" } : Car
这似乎对我有用。这不是记录,而是 class。也可能不能命名为POCO。
//file DataModel.fs
module DataModel
open System.ComponentModel.DataAnnotations
open System.Data.Entity
type Car()=
[<Key>]
member val Id = 0 with get,set
member val Name = "" with get,set
type public CLCars() =
inherit DbContext()
member val Cars: DbSet<Car> = base.Set<Car>() with get,set
//file Program.fs
module Program
open DataModel
let db = new CLCars()
let c = new Car(Name="Honda")
db.Cars.Add(c) |> ignore
db.SaveChanges() |> ignore