我试图将Entity Framework Core示例翻译为F#。主要是我创建了类BloggingContext
,Blog
和Post
的F#版本,如下所示:
namespace ConsoleApp.SQLite.FSharp
open Microsoft.EntityFrameworkCore
open System.Collections.Generic
type Post() =
member val PostId = 0 with get,set
member val Title = "" with get,set
member val Content = "" with get,set
member val BlogId = 0 with get,set
member val Blog = Blog() with get,set
and Blog() =
member val BlogId = 0 with get,set
member val Url = "" with get,set
member val Posts = [Post()] with get,set
type BloggingContext() =
inherit DbContext()
member val Blogs : DbSet<Blog> = null with get,set
member val Posts : DbSet<Post> = null with get,set
override this.OnConfiguring optionsBuilder =
optionsBuilder.UseSqlite("Data Source=blogging.db") |> ignore
然后,我尝试创建Main
类的f#版本,我写的如下:
open System
open ConsoleApp.SQLite.FSharp
[<EntryPoint>]
let main argv =
let db = new BloggingContext() in
let blog = Blog() in
printfn "%s" "Stopped working before here";
blog.Url <- "http://blogs.msdn.com/adonet";
db.Blogs.Add(blog) |> ignore;
let count : int = db.SaveChanges() in
printfn "%i records saved to database" count;
printfn "All blogs in database:"
for blog in db.Blogs do
printfn "- %s" blog.Url
0
因为它在代码中被标记,它只是在这行代码let blog = Blog() in
之后停止打印任何内容(以及停止工作)并且我不知道我做错了什么创建一个Blog
实例。
答案 0 :(得分:2)
我通过删除
行中的Post()
来解决此问题
member val Posts = [Post()] with get,set
然后
member val Posts = [] with get,set
问题是类声明中的冗余,我正在创建一个已经有一个成员的列表,这不是我想要的。