<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason Rowe &#187; .net4</title>
	<atom:link href="http://jasonrowe.com/tag/net4/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonrowe.com</link>
	<description>Digging into the Details</description>
	<lastBuildDate>Fri, 23 Jul 2010 04:40:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Entity Framework &#8211; Code Only Looking Good</title>
		<link>http://jasonrowe.com/2009/12/11/entity-framework-code-only/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=entity-framework-code-only</link>
		<comments>http://jasonrowe.com/2009/12/11/entity-framework-code-only/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 22:53:36 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[.net4]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=765</guid>
		<description><![CDATA[I&#8217;ve been hearing rumors Entity Framework (EF) is more robust now. I did a few searches and found the latest version EF CTP2. I was happy to find EF has a &#8220;code only&#8221; method for generating the database schema and &#8230; <a href="http://jasonrowe.com/2009/12/11/entity-framework-code-only/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been hearing rumors Entity Framework (EF) is more robust now. I did a few searches and found the latest version <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=13FDFCE4-7F92-438F-8058-B5B4041D0F01&#038;displayLang=en">EF CTP2.</a>  I was happy to find EF has a &#8220;code only&#8221; method for generating the database schema and a good take on configuration. </p>
<p>If you are intrested in the code only method check out this <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">walk through</a>.  I went through it and built my own application to get a handle on this new feature. Here were the steps I took to get up and running.  You will need Visual Studio 2010, SQL, and CTP2 version of EF.</p>
<p><strong>1)</strong> Install <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=13FDFCE4-7F92-438F-8058-B5B4041D0F01&#038;displayLang=en">CTP2 Entity Framework Preview 2</a></p>
<p><a href="http://jasonrowe.com/2009/12/11/entity-framework-code-only/ctp2-2/" rel="attachment wp-att-792"><img src="http://jasonrowe.com/wp-content/uploads/2009/12/ctp21-300x235.PNG" alt="ctp2" title="ctp2" width="300" height="235" class="alignnone size-medium wp-image-792" /></a></p>
<p><strong>2)</strong> Create a new class library project in your solution. This will be for your objects that will get mapped to the database.  I named mine &#8220;UserSchema&#8221;.  In this project you are just creating normal classes without any references to EF.  Below are the two classes I used for testing.  Notice they have no meta tags or any other tags that would be EF specific.</p>
<pre class="brush: c#; ">

   public class Profile
    {
        public Profile() { }
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string PermaUrl { get; set; }
        public DateTime Created { get; set; }
        public DateTime? LastSeen { get; set; }
    }

    public class User
    {
        public User() { }
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string UserName { get; set; }
        public string Password {get; set;}
        public ICollection&lt;profile&gt; Profiles { get; set; }
    }
</pre>
<p><strong>3)</strong> Next create a new class project in your solution for your EF models.  I called mine UserManager.Model.  I think it is good to make a seperate project as this is going to setup all your class context for connecting to EF. You will also need to add a project reference to the project that has your objects which you want to be mapped in the database and a reference to Microsoft.Data.Entity.CTP and System.Data.Entity.</p>
<pre>
public class UserModel : ObjectContext
    {
        public UserModel(EntityConnection connection)
            : base(connection)
        {
            DefaultContainerName = "UserModel";
        }

        public IObjectSet<user> User
        {
            get { return base.CreateObjectSet</user><user>(); }
        }

        public IObjectSet
<profile> Profiles
        {
            get { return base.CreateObjectSet</profile>
<profile>(); }
        }
    }
</profile></user></pre>
<p><strong>4)</strong> Create a new console application to automatically generate your database.  I called mine &#8220;UserManager.GenerateDatabase&#8221;.  This is the application that will use the object context to autogenerate the database. You can also do specific table configuration and other initialization.  You can use a RegisterConfigurations method.  The walk through above goes into more detail on that.</p>
<pre>
var builder = new ContextBuilder<usermodel>();
var connStr = ConfigurationManager.ConnectionStrings["UserDBConn"].ConnectionString;

var connection = new SqlConnection(connStr);

using (var ctx = builder.Create(connection))
{
    if (ctx.DatabaseExists())
        ctx.DeleteDatabase();
     ctx.CreateDatabase();
</usermodel></pre>
<p>My project and auto generated tables ended up looking like this:<br />
<a href="http://jasonrowe.com/wp-content/uploads/2009/12/sql_result1.png"><img src="http://jasonrowe.com/wp-content/uploads/2009/12/sql_result1.png" alt="sql results from code only" title="sql_result" width="211" height="309" class="alignnone size-full wp-image-848" /></a></p>
<p><a href="http://jasonrowe.com/2009/12/11/entity-framework-code-only/efprojectsetup/" rel="attachment wp-att-784"><img src="http://jasonrowe.com/wp-content/uploads/2009/12/EFProjectSetup-262x300.PNG" alt="EFProjectSetup" title="EFProjectSetup" width="262" height="300" class="alignnone size-medium wp-image-784" /></a></p>
<p>So after going through this quick setup I got a taste of what is to come.  I&#8217;m still going to stick with ActiveRecord for my ORM but I think EF will definitly be an option in the future.  Also, the code only mode will be a little more comfortable for people who have an ActiveRecord background. </p>
<p><strong><em>update</em></strong> &#8211; <a href="http://jasonrowe.com/2009/12/22/entity-framework-code-only-looking-good-part-2/">Code Only Looking Good (part 2)</a></p>
<p><strong><em>update CTP3</em></strong> &#8211; <a href="http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/">Code First Castle Windsor</a></p>
<p>Entity Framework blog posts</p>
<ol>
<li><a href="http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx">Code Only Announcement</a></li>
<li><a href="http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-entity-framework.aspx">Code Only Walk Through Entity FrameWork</a></li>
<li><a href="http://blogs.msdn.com/efdesign/archive/2009/08/03/code-only-enhancements.aspx">Code Only Enhancements</a></li>
<li><a href="http://blogs.msdn.com/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx">Even More Code Only Enhancements!</a></li>
</ol>
</profile>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2009/12/11/entity-framework-code-only/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
