Reputation: 4302
As a follow-up to this question, I now want to add a nested type. I created the nested type with the following code:
[<CLIMutable>]
type Child = {[<Key>]Id:int; FirstName:string; Gender:string; Grade:int}
[<CLIMutable>]
type Family = {[<Key>]Id:int; LastName:string; IsRegistered:bool; Children: Child list}
type CLFamily() =
inherit DbContext()
[<DefaultValue>]
val mutable m_families: DbSet<Family>
[<DefaultValue>]
val mutable m_children: DbSet<Child>
member public this.Families with get() = this.m_families
and set v = this.m_families <- v
member public this.Children with get() = this.m_children
and set v = this.m_children <- v
let dbFamily = new CLFamily()
let connectionString = "Server=.;Database=FamilyDomain;Trusted_Connection=True;"
dbFamily.Database.Connection.ConnectionString <- connectionString
let family = {Id=0;LastName="Test";IsRegistered=true;
Children=[{Id=0; FirstName="Test"; Gender="Male"; Grade=5}];}
dbFamily.Families.Add(family) |> ignore
dbFamily.SaveChanges() |> ignore
The problems is that the Children table is not being created by the code-gen. I was expecting that the table would be created, a foreign key defined, and the values populated.
Upvotes: 2
Views: 1014
Reputation: 110
EF will create a database for you the first time you run, but change to the structure of your model wont be propagated to the database with out migrations. Normally, this means running some extra commands from the package manager console which generates code and other commands to run those model updates against the database. Given that the code generated is probably C#, this might cause problems in your F# app.
If your still early in the process and don't care about your data being deleted, dropping the database and letting EF recreate it will get your schema updated.
In similar cases I have put a:
d.Database.CreateIfNotExists() |> ignore
somewhere early in my code to force it to be created after I drop it, but I don't actually remember if that's needed or not.
I'm sure there are better tutorials out there, but this link shows how to perform the migrations in a C# project. Given all the other places where Visual Studio doesn't treat F# as a first class language, I don't expect that to actually work, but I'd be happy to be proven wrong.
I've always just dropped the db and recreated it, but my projects haven't gotten to the point nor were of the type where loosing data really mattered to me.
Additionally, if you do some searching for examples of code first migrations and look at the kind of code that the code generator creates, it would be possible for you to write all of that manually. Tedious, but possible. But it would keep all your code in F#.
Upvotes: 2