<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>ADO.NET Entity Framework</title>
        <link>http://geekswithblogs.net/kobush/category/7047.aspx</link>
        <description>ADO.NET Entity Framework</description>
        <language>pl-PL</language>
        <copyright>Szymon Kobalczyk</copyright>
        <managingEditor>skobalczyk@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Getting started with ADO.NET Entity Framework. Part I: Mapping entities</title>
            <link>http://geekswithblogs.net/kobush/archive/2007/08/29/GettingStartedWithEntityFramework_1.aspx</link>
            <description>&lt;p&gt;Two months ago, after listening to &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=226"&gt;.NET Rocks! show with Daniel Simmons&lt;/a&gt;, I was really curious to learn more about ADO.NET Entity Framework (EF). As part of my learning I was ready to write a tutorial on how to get started with this framework. However there were no tools available at the time and with instructions on how to manually edit XML files the article turned very long, and quite frankly looked very boring. Hence I decided to postpone publishing it and wait for the tools to came out. &lt;/p&gt;
&lt;p&gt;This week Microsoft released &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=F1ADC5D1-A42E-40A6-A68C-A42EE11186F7&amp;amp;displaylang=en"&gt;ADO.NET Entity Framework Beta 2&lt;/a&gt;. You can find the complete release notes on the &lt;a href="http://blogs.msdn.com/adonet/archive/2007/08/27/entity-framework-beta-2-the-1st-entity-framework-tools-ctp-released.aspx"&gt;ADO.NET Team Blog&lt;/a&gt; so I won’t repeat it here. What’s more important we finally get &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=09A36081-5ED1-4648-B995-6239D0B77CB5&amp;amp;displaylang=en"&gt;the first CTP of Entity Framework Tools&lt;/a&gt;. So now I have all the pieces to attempt to write my tutorial again. &lt;/p&gt;
&lt;p align="center"&gt;• • •&lt;/p&gt;
&lt;p&gt;Microsoft went a long road to get into the ORM space starting with ObjectSpaces, then WinFS and now LINQ and Entity Framework. For those interested in the full story here is &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/05/31/the-origin-of-linq-to-sql.aspx"&gt;nice article from one of the authors&lt;/a&gt;. Even though I'm by no means an ORM expert, I've worked before with number of both commercial and open source systems falling into this category. &lt;/p&gt;
&lt;p&gt;According to ADO.NET team Entity Framework is much more then just another ORM tool. The aim here is to solve a more generic problem of mapping conceptual model to application logic which is prevalent not only in storing programming objects but also in such areas as data replication, remoting and reporting. Which EF you maintain a single conceptual model of your domain objects (called entities here) that can be later mapped and reused with other representation.&lt;/p&gt;
&lt;p&gt;To learn new technology I usually develop a sample project so I have some well defined goal to achieve. This time again I choose to work with the &lt;a href="http://msdn2.microsoft.com/en-us/library/aa479071.aspx"&gt;PetShop 4.0 sample application&lt;/a&gt;. I prefer this sample for several reasons:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;It contains existing entity model, so nice thing to try would if the whole data access code can be replaced with EF. &lt;/li&gt;
    &lt;li&gt;The application uses four databases in total, so it would test how to map the entity model to multiple data sources. &lt;/li&gt;
    &lt;li&gt;It is relatively simple and I worked with it before when &lt;a href="http://geekswithblogs.net/kobush/category/3702.aspx"&gt;learning CAB&lt;/a&gt;. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Besides Northwind and Adventure Works are already mapped as EF samples so it would be no fun in using them. &lt;/p&gt;
&lt;h3&gt;Step 1. Generating entity model from database&lt;/h3&gt;
&lt;p&gt;Working with Entity Framework involves creating three schemas for your data: &lt;/p&gt;
&lt;p&gt;1. &lt;strong&gt;Conceptual Schema (CSDL)&lt;/strong&gt; — represents the conceptual model of your data, that is entities and their relationships (in current version this maps 1:1 to the generated object model).  &lt;/p&gt;
&lt;p&gt;2. &lt;strong&gt;Storage Metadata Schema (SSDL)&lt;/strong&gt; — describes the representation of your data when its stored in the database (or other storage media). &lt;/p&gt;
&lt;p&gt;3. &lt;strong&gt;Mapping Specification (MSL)&lt;/strong&gt; — as you can already guess this one describes how to map data from one of the above schemas to the another. &lt;/p&gt;
&lt;p&gt;All three are now defined as part of single XML document that will be part of your project (&lt;strong&gt;edmx&lt;/strong&gt;). &lt;/p&gt;
&lt;p&gt;The normal workflow for Entity Framework should start with first creating the concept model of entities and then work the storage and mapping schemas from there. However in many cases you will be working with existing applications, so it would be more convenient to start from the database schema instead. This release of EF targets this "data first" scenario with the &lt;strong&gt;EDM Wizard &lt;/strong&gt;built in Visual Studio. &lt;/p&gt;
&lt;p&gt;As I said before PetShop uses four databases: product inventory, orders, and two for ASP.NET users and profiles. For now I will concentrate on the first one. So to begin with let's see how this database looks like: &lt;/p&gt;
&lt;p align="center"&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="280" alt="PetShop 4 Inventory Database Diagram" width="680" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/petshop_inventory_db_1.jpg" /&gt; &lt;/p&gt;
&lt;p&gt;As you see this database is extremely simple: each category has multiple products, that in turn hold multiple items. Each product item points to its supplier and there is additional table that holds current inventory count. &lt;/p&gt;
&lt;p&gt;Okay, I'm ready to create the entity model. First I've created a new C# class library project, and then from the &lt;strong&gt;Add New Item&lt;/strong&gt; dialog selected the &lt;strong&gt;ADO.NET Entity Data Model&lt;/strong&gt; template. This starts the &lt;strong&gt;Entity Data Model Wizard &lt;/strong&gt;where in first step we select either to generate model from database or to create empty one: &lt;/p&gt;
&lt;p align="center"&gt; &lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="505" alt="EDM Wizard: Step 1" width="544" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_7.png" /&gt; &lt;/p&gt;
&lt;p&gt;After selecting the first option we go to the next step when we need to specify the connection string to our database:&lt;/p&gt;
&lt;p align="center"&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="505" alt="EDM Wizard: Step 2" width="544" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_8.png" /&gt; &lt;/p&gt;
&lt;p&gt;Note that the connection string used by the EF besides the typical settings for ADO.NET also contains paths to all three schema files.  &lt;/p&gt;
&lt;p&gt;In the last step we can select which tables, views and stored procedures should be included in the generated model (in this case I want to include all tables besides the &lt;strong&gt;AspNet_SqlCacheTablesForChangeNotification&lt;/strong&gt;). We can also specify the namespace for the generated classes:&lt;/p&gt;
&lt;a atomicselection="true" href="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_3.png"&gt;&lt;/a&gt;
&lt;p align="center"&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="505" alt="EDM Wizard: Step 3" width="544" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_9.png" /&gt; &lt;/p&gt;
&lt;p&gt;After pressing the &lt;strong&gt;Finish &lt;/strong&gt;button the wizard generates the entity model (the PetShopModel.edmx file) and adds all required references to the project. In the previous version the wizard &lt;a href="http://www.sqlskills.com/blogs/bobb/2007/04/22/EDMWizardNotWorkingUseEDMGen.aspx"&gt;didn't worked very well&lt;/a&gt; and you'd had to use the command line tool called &lt;strong&gt;EDM Generator &lt;/strong&gt;(edmgen.exe) to get these files. This time things changed for good so lets look at what was generated. &lt;/p&gt;
&lt;p&gt;When we open this file from the Solution Explorer we can finally see our enities inside the &lt;strong&gt;EDM Designer &lt;/strong&gt;(click on image to enlarge):&lt;/p&gt;
&lt;p align="center"&gt;&lt;a target="_blank" atomicselection="true" href="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_6.png"&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="476" alt="EDM Designer" width="640" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/kobush/WindowsLiveWriter/GettingstartedwithA.PartIMappingentities_BF5B/image_thumb_6.png" /&gt; &lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice that although this model looks very similar to the database model shown earlier it actually shows entities not tables. So besides the &lt;strong&gt;scalar properties &lt;/strong&gt;that are mapped to the table columns we also have the &lt;strong&gt;navigational properties &lt;/strong&gt;mapped to table relationships. There is also a new tab next to the Solution Explorer called &lt;strong&gt;Entity Model Browser &lt;/strong&gt;that displays the structure of the model. We can see here that this model includes both the conceptual (entity) and storage schema. Finally at the bottom we see new window titled &lt;strong&gt;Entity Mapping Details&lt;/strong&gt; that displays how the two schemas are mapped. &lt;/p&gt;
&lt;p&gt;In the second part I will look at how we can modify the generated model to align it with our concept model and at the same time learn how to use this designer. Stay tuned!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115048"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115048" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/kobush/aggbug/115048.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Szymon Kobalczyk</dc:creator>
            <guid>http://geekswithblogs.net/kobush/archive/2007/08/29/GettingStartedWithEntityFramework_1.aspx</guid>
            <pubDate>Wed, 29 Aug 2007 18:30:22 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/kobush/comments/115048.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/kobush/archive/2007/08/29/GettingStartedWithEntityFramework_1.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/kobush/comments/commentRss/115048.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/kobush/services/trackbacks/115048.aspx</trackback:ping>
        </item>
    </channel>
</rss>