我试图在F#中使用EF
这些包是EntityFramework 6.1.3
和Mysql.Data.Entity 6.9.8
。
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
<package id="MySql.Data" version="6.9.8" targetFramework="net452" />
<package id="MySql.Data.Entity" version="6.9.8" targetFramework="net452" />
</packages>
使用EntityFramework 6.0.0
时很奇怪
(如果我只使用Mysql.Data.Entity 6.9.8
,NuGet告诉我卸载6.0.0失败并让我重启VS(这是另一个问题))。
代码:
open System.Data.Entity
open MySql.Data.Entity
open System.ComponentModel.DataAnnotations.Schema
open System.ComponentModel.DataAnnotations
[<Table("customer")>]
type Customer() =
[<Key>]
member val id = 0 with get,set
member val name = "" with get,set
member val contact = "" with get,set
member val telephone = "" with get,set
member val email = "" with get,set
member val remark = "" with get,set
override this.ToString() =
sprintf "name:%s contact:%s" this.name this.contact
type Demo() =
inherit DbContext("MyContext")
let mutable c : DbSet<Customer> = null
member this.Customer with get () = printfn "from get method : %A" c; c
and set (newC:DbSet<Customer>) =
c <- newC
printfn "from set method : %A" (c.ToListAsync().Result)
printfn "from set method :%A" c
[<EntryPoint>]
let main argv =
use c = new Demo()
printfn "%A" c.Database.Connection.ConnectionString
printfn "%A" c.Customer
0 // return an integer exit code
输出很有意思:
from set method : seq [name:customer1 contact:Jack; name:customer2 contact:Rose]
from set method :<seq>
"server=localhost;port=3306;database=demo;user id=cc"
from get method : <null>
<null>
app.conf:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="MyContext" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;Port=3306;Database=demo;Uid=cc;Pwd=xxx;" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
配置来自C#项目(使用相同的软件包,因为6.0.0适用于C#;我首先安装Mysql.Data.Entity
并将实体框架更新到6.1.3)。因为原来的F#app.conf
根本无法运作。 (缺少section name="entityFramework"
...)。
我不得不说在F#中使用EF比C#更难。