{"id":1613,"date":"2011-04-04T13:06:00","date_gmt":"2011-04-04T13:06:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/2011\/04\/04\/f-code-first-development-with-entity-framework-4-1\/"},"modified":"2022-10-12T14:03:35","modified_gmt":"2022-10-12T21:03:35","slug":"f-code-first-development-with-entity-framework-4-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/f-code-first-development-with-entity-framework-4-1\/","title":{"rendered":"F# Code-First Development with Entity Framework 4.1"},"content":{"rendered":"<p><span style=\"font-size: small;\">By Jack Hu<\/span><\/p>\n<p><span style=\"font-size: small;\">As the word \u201ccode-first\u201d implies, the EF 4.1 offers a code centric data programming paradigm. From a coder\u2019s point of view, it requires little effort to map a very clean object model to a database. This style of programming is idea for explorative, bottom-up kind fsharp programmers. Since EF 4 CTP4 release, there have already been many buzzes. The following two blogs have in-depth EF 4.1 feature overview. <\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/adonet\/\"><span style=\"font-size: small;\">EF 4.1 Model &amp; Database First Walkthrough<\/span><\/a><\/li>\n<li><a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2010\/07\/16\/code-first-development-with-entity-framework-4.aspx\"><span style=\"font-size: small;\">Code-First Development with Entity Framework 4<\/span><\/a><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\">In this blog, I demonstrate how to use EF 4.1 Code-first in Fsharp 2.0 to save a record into a SQL CE. I also provide workarounds for several practical issues may block fsharp programmers. <\/span><\/p>\n<p><b><span style=\"font-size: small;\">Step0: Install Software Packages <\/span><\/b><\/p>\n<ul>\n<li><a href=\"http:\/\/www.microsoft.com\/downloads\/en\/details.aspx?FamilyID=2dc5ddac-5a96-48b2-878d-b9f49d87569a&amp;displaylang=en\"><span style=\"font-size: small;\">ADO.NET Entity Framework 4.1 Release Candidate<\/span><\/a><\/li>\n<li><a href=\"http:\/\/www.microsoft.com\/downloads\/en\/details.aspx?FamilyID=033cfb76-5382-44fb-bc7e-b3c8174832e2\"><span style=\"font-size: small;\">Microsoft SQL Server Compact 4.0<\/span><\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/sqlservercompact\/\"><span style=\"font-size: small;\">SQL Server Compact 4.0 Tooling for VS2010<\/span><\/a><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\"><b>Step1: Create a New F# Application<\/b> <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/2018.image001_thumb_4BD4C3ED.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222168\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/2018.image001_thumb_4BD4C3ED.png\" alt=\"f sharp application\" width=\"599\" height=\"409\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/2018.image001_thumb_4BD4C3ED.png 599w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/2018.image001_thumb_4BD4C3ED-300x205.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><\/p>\n<p><span style=\"font-size: x-small;\"><span style=\"font-size: small;\"><span style=\"text-decoration: underline;\">Add Project references<\/span>: Unlike C#, Fsharp compiler requires additional references from System.Data and System.Data.Entity to resolve base types inherited by EF 4.1 <\/span><\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1263.image002_thumb_326CC0B3.png\"><span style=\"font-size: small;\"><img decoding=\"async\" title=\"image002\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1263.image002_thumb_326CC0B3.png\" alt=\"image002\" width=\"387\" height=\"341\" border=\"0\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: small;\">The EntityFramework.dll is under $\\Program Files (x86)\\Microsoft ADO.NET Entity Framework Feature CTP5\\Binaries\\EntityFramework.dll <\/span><\/p>\n<p><span style=\"font-size: small;\"><b>Step2: Create a Model<\/b> <\/span><\/p>\n<p><span style=\"font-size: small;\">In this example, I create a CLCars class. The class contains a single DbSet of Cars. In Car class I have a field of ID (serve as the primary key) and a field of Name. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/3007.image003_thumb_4AFC5E03.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222165\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/3007.image003_thumb_4AFC5E03.png\" alt=\"car class\" width=\"731\" height=\"573\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/3007.image003_thumb_4AFC5E03.png 731w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/3007.image003_thumb_4AFC5E03-300x235.png 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">In the driver code, I create a new CLCars database, added a Car to it and flush out to the database. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/8875.image004_thumb_787D7DC6.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222169\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/8875.image004_thumb_787D7DC6.png\" alt=\"class\" width=\"416\" height=\"281\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/8875.image004_thumb_787D7DC6.png 416w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/8875.image004_thumb_787D7DC6-300x203.png 300w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\"><b>Step3: Pointing to a Database<\/b> <\/span><\/p>\n<p><span style=\"font-size: small;\">I love SQL CE 4.0, especially because user no longer needs to generate a primary key while adding a record. Yeah! In this example, I add an app.config file and embed the database connection string. I also make sure the connectionString Name property is the same as my DbContext class name, so that the EF 4.1 automatically generates a database at runtime. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/7612.image005_thumb_631FC85E.png\"><span style=\"font-size: small;\"><img decoding=\"async\" title=\"image005\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/7612.image005_thumb_631FC85E.png\" alt=\"image005\" width=\"567\" height=\"240\" border=\"0\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: small;\"><b>Step4: Run and Verify data<\/b> <\/span><\/p>\n<p><span style=\"font-size: small;\">After build and run app, I use <\/span><a href=\"https:\/\/learn.microsoft.com\/en-us\/archive\/blogs\/sqlservercompact\/\"><span style=\"font-size: small;\">SQL Server Compact 4.0 Tooling for VS2010<\/span><\/a><span style=\"font-size: small;\"> to verify the database, the table and the data are added correctly. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0383.image006_thumb_10A0E822.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222170\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0383.image006_thumb_10A0E822.png\" alt=\"clcars database\" width=\"692\" height=\"358\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0383.image006_thumb_10A0E822.png 692w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0383.image006_thumb_10A0E822-300x155.png 300w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">During the ad-hocking, I also encountered several issues. Here is how I work around them. <\/span><\/p>\n<p><span style=\"font-size: small;\"><b>Issue#1: EF 4.1 does not support nested type; the model can NOT be used inside a Module or a Script<\/b> <\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: small;\">Trying to put the model inside a module, I got a run time exception: \u201cThe type &#8216;Program+Car&#8217; is not a supported entity type.\u201d <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6237.image007_thumb_29308572.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222171\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6237.image007_thumb_29308572.png\" alt=\"database\" width=\"880\" height=\"246\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6237.image007_thumb_29308572.png 880w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6237.image007_thumb_29308572-300x84.png 300w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6237.image007_thumb_29308572-768x215.png 768w\" sizes=\"(max-width: 880px) 100vw, 880px\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">Digging into fsharp assembly using reflector, we can see the Fsharp script and module generate a nested-type which is not supported in EF 4.1 <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0777.image008_thumb_41C022C2.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222172\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0777.image008_thumb_41C022C2.png\" alt=\"image 008\" width=\"562\" height=\"389\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0777.image008_thumb_41C022C2.png 562w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/0777.image008_thumb_41C022C2-300x208.png 300w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\">The work around is to put the model code inside a namespace DataModel <\/span><\/p>\n<p><span style=\"font-size: small;\">The VB module shares the same limitation. The work around in below example is to move the Car and CLCars class outside the Module1. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6052.image009_thumb_4C532ED3.png\"><span style=\"font-size: small;\"><img decoding=\"async\" style=\"padding-top: 0px; padding-right: 0px; padding-left: 0px; border-width: 0px;\" title=\"image009\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6052.image009_thumb_4C532ED3.png\" alt=\"image009\" width=\"850\" height=\"538\" border=\"0\" \/><\/span><\/a><\/p>\n<p><b><span style=\"font-size: small;\">Issue#2: Property initialization on DBContext class need to use [&lt;<\/span><\/b><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee353743.aspx\"><b><span style=\"font-size: small;\">DefaultValue<\/span><\/b><\/a><b><span style=\"font-size: small;\">&gt;] attribute with <\/span><\/b><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd469494.aspx\"><b><span style=\"font-size: small;\">Explicit Fields<\/span><\/b><\/a><b><\/b><\/p>\n<p><span style=\"font-size: small;\">During EF DbContext construction, it will reflect on all its properties and initialized them with database mapping values. After base construction, if the inhered Fsharp class calls its constructor with property initialization, it will override the property value that base class already initialized. <\/span><\/p>\n<p><span style=\"font-size: small;\">In the following example, the DbContext constructor will initialize m_cars to a DbSet&lt;Car&gt; mapping value, but the inhered CLCars constructor will reinitialize n_cars to NULL. This could cause a NullReferenceException at runtime. <\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1777.image010_thumb_79D44E96.png\"><img decoding=\"async\" class=\"alignleft size-full wp-image-222173\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1777.image010_thumb_79D44E96.png\" alt=\"010\" width=\"570\" height=\"188\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1777.image010_thumb_79D44E96.png 570w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/1777.image010_thumb_79D44E96-300x99.png 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6064.image011_thumb_2B5FBC2C.png\"><span style=\"font-size: small;\"><img decoding=\"async\" style=\"padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;\" title=\"image011\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2011\/04\/6064.image011_thumb_2B5FBC2C.png\" alt=\"image011\" width=\"824\" height=\"386\" border=\"0\" \/><\/span><\/a><\/p>\n<p><span style=\"font-size: small;\">EF 4.1 code-first is a great tool for F# 2.0 data programming. As Fsharp 3.0 features become clearer in the next few months, I am supper exited and feel good about them. I expect Fsharp 3.0 will take these experiences to the next level. Happy coding!\u00a0 <\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: small;\">Code example is published @ <\/span><a title=\"http:\/\/code.msdn.microsoft.com\/F-Code-First-Development-326dede1\" href=\"http:\/\/code.msdn.microsoft.com\/F-Code-First-Development-326dede1\"><span style=\"font-size: small;\">http:\/\/code.msdn.microsoft.com\/F-Code-First-Development-326dede1<\/span><\/a><\/p>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Jack Hu As the word \u201ccode-first\u201d implies, the EF 4.1 offers a code centric data programming paradigm. From a coder\u2019s point of view, it requires little effort to map a very clean object model to a database. This style of programming is idea for explorative, bottom-up kind fsharp programmers. Since EF 4 CTP4 release, [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155,1029],"tags":[85,5,547,660,357,13],"class_list":["post-1613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","category-web","tag-asp-net","tag-csharp","tag-f","tag-fsharp","tag-sql","tag-visual-studio-2010"],"acf":[],"blog_post_summary":"<p>By Jack Hu As the word \u201ccode-first\u201d implies, the EF 4.1 offers a code centric data programming paradigm. From a coder\u2019s point of view, it requires little effort to map a very clean object model to a database. This style of programming is idea for explorative, bottom-up kind fsharp programmers. Since EF 4 CTP4 release, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/1613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=1613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/1613\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=1613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=1613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=1613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}