<?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>enjoying the web</description>
	<lastBuildDate>Sun, 13 May 2012 14:05:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</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&#038;utm_medium=rss&#038;utm_campaign=entity-framework-code-only</link>
		<comments>http://jasonrowe.com/2009/12/11/entity-framework-code-only/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 04: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[The release described in this post is no longer current. Entity Framework 4.1 RTW is now available. 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. &#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[<strong>

<blockquote>
<p>
<font color="#ff0000">The release described in this post is no longer current. </font>
</p>

<a href="http://blogs.msdn.com/b/adonet/archive/2011/04/11/ef-4-1-released.aspx">
<font size="5">Entity Framework 4.1 RTW</font>
</a>
<font color="#ff0000" size="5">is now available</font>.
</blockquote>

</strong>
<br />

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. 

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.

<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>

<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>

<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.

<pre class="brush: csharp; title: ; notranslate">
   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>

<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.

<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>

<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.

<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>

My project and auto generated tables ended up looking like this:
<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>


<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>

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. 


Entity Framework blog posts
<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>

