<?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; Entity Framework</title>
	<atom:link href="http://jasonrowe.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonrowe.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Sep 2010 04:53:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Top 5 Features of Code First Entity Framework CTP4</title>
		<link>http://jasonrowe.com/2010/07/18/top-5-features-of-code-first-entity-framework-ctp4/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=top-5-features-of-code-first-entity-framework-ctp4</link>
		<comments>http://jasonrowe.com/2010/07/18/top-5-features-of-code-first-entity-framework-ctp4/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 16:19:26 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/2010/07/18/top-5-features-of-code-first-entity-framework-ctp4/</guid>
		<description><![CDATA[&#160; The latest release of Entity Framework CTP4 has some nice but unexpected changes. They introduced DbContext and DbSet to simplify model caching, database provisioning, schema creation and connection management. Here are my top 5 favorite features of the release. &#8230; <a href="http://jasonrowe.com/2010/07/18/top-5-features-of-code-first-entity-framework-ctp4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>The latest release of Entity Framework CTP4 has some nice but unexpected changes. They introduced DbContext and DbSet to simplify model caching, database provisioning, schema creation and connection management. Here are my top 5 favorite features of the release.</p>
<ol>
<li><strong><em>Less code and fewer Concepts.</em></strong> The coolest part is the use of convention over configuration. See the <a href="http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4piwalkthrough.aspx">Productivity Improvements</a> walk through and <a href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx">Conventions for code first</a>. I like the extra touch that DbContext&#160; will create the database for you if you want. No code to write. Just use the default CreateDatabaseOnlyIfNotExists. </li>
<li><em><strong>Simplified Database Experience.</strong></em> With the introduction of DbContext and DbSet. EF now takes care of database provisioning, schema creation and connection management. Nice! My favorite part is the Interface IDbConnectionFactory to allow creation of your own conventions. </li>
<li><strong>Backwards Compatible.</strong> Yes, this is a feature when it comes to CTP’s and Betas. Some projects might just throw stuff away or completely change things on you. EF allows you to still use ObjectContext or <a href="http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx">Data Annotations</a> for advanced scenarios where common conventions don’t work for you. </li>
<li><strong>Simplified Configuration Experience.</strong> As I mentioned you can use annotations for this but you also have another cool option. DbContext includes a virtual method that can be overridden to do configuration right in the class. This means no need for separate configuration classes now. I definitely like having it all in one place. Much cleaner and easy to read. </li>
<li><strong>Testability </strong>IDbSet&lt;TEntity&gt; allows you to define an interface that can be implemented by your derived context. This allows the context to be replaced with an in-memory test double for testing. </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2010/07/18/top-5-features-of-code-first-entity-framework-ctp4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EF CTP3 Code First and Castle Windsor</title>
		<link>http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=ef-ctp3-code-first-and-castle-windsor</link>
		<comments>http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 04:31:28 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Code First]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1280</guid>
		<description><![CDATA[Awhile back I wrote a simple Holiday service to try out Entity Framework &#34;Code First&#34; (the new name for Code Only). I&#8217;ve upgraded the bits and tried out some of the new features of CTP3 using this walkthrough as a &#8230; <a href="http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Awhile back I wrote a simple Holiday service to try out Entity Framework &quot;Code First&quot; (the new name for Code Only). I&#8217;ve upgraded the bits and tried out some of the new features of CTP3 <a href="http://blogs.msdn.com/b/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">using this walkthrough as a guide</a>. You can download my sample app from <a href="http://github.com/JasonRowe/EF-CTP3-HolidayService/zipball/master">git hub</a>.</p>
<p>In addition, I added Inversion of Control (IOC) using Castle Windsor and implemented a repository pattern. </p>
<p>My database generation script and test data now look like this.</p>
<pre class="brush: c#; ">

databasefactory.Create();
var repository = new HolidayRepository(databasefactory);

//... build up test data for xmas and newyear

repository.Add(xmas);
repository.Add(newYear);

//commit changes
var unitOfWork = new UnitOfWork(databasefactory);
unitOfWork.Commit();
</pre>
<p>The other interesting part of this sample project is using WCF and EF together and WCF dependency injection using Castle Windsor.</p>
<p>First I setup my interfaces and configuration files for EF and WCF.</p>
<pre class="brush: c#; ">

[DataContract]
public class Holiday : IEntity
{
    public long Id { get; set; }

    [DataMember]
    public string HolidayName { get; set; }

    [DataMember]
    public DateTime HolidayDate { get; set; }

    [DataMember]
    public ICollection&lt;holidayevent&gt; HolidayEvents {get; set;}
}
</pre>
<pre class="brush: c#; ">

[DataContract]
public class HolidayEvent : IEntity
{
    public long Id { get; set; }

    [DataMember]
    public string EventName { get; set; }

    [DataMember]
    public Holiday Holiday { get; set; }

    [DataMember]
    public long HolidayId { get; set; }
}
</pre>
<pre class="brush: c#; ">

public HolidayConfiguration()
{
    Property(h =&gt; h.Id).IsIdentity();
    Property(h =&gt; h.HolidayName).HasMaxLength(1000).IsRequired();

    Relationship(e =&gt; e.HolidayEvents).FromProperty(h =&gt; h.Holiday);
}
</pre>
<pre class="brush: c#; ">

public HolidayEventConfiguration()
{
    Property(e =&gt; e.Id).IsIdentity();
    Property(e =&gt; e.EventName).HasMaxLength(1000).IsRequired();

    //HolidayId is a FK property and Holiday is a navigation property backed by this FK
    Relationship(e =&gt; e.Holiday).FromProperty(h =&gt; h.HolidayEvents).HasConstraint((e, h) =&gt; e.HolidayId == h.Id);
}
</pre>
<p>In my .svc file I use a custom factory to do dependency injection</p>
<pre class="brush: c#; ">

Factory=&quot;Castle.Facilities.WcfIntegration.WindsorServiceHostFactory, Castle.Facilities.WcfIntegration&quot;
</pre>
<p>On startup, I add my HolidayService as a component and add the DatabaseFactory as a parameter. The DatabaseFactory is used to setup my EF Object Context.</p>
<pre class="brush: c#; ">

container.Register(Component.For&lt;iholidayservice&gt;().ImplementedBy&lt;holidayservices&gt;().DependsOn
    (Property.ForKey(&quot;databaseFactory&quot;).Eq(databaseFactory)));
</pre>
<p>A great resource for seeing many of the new features in EF CTP3 is the <a href="http://shrinkr.codeplex.com/">shrinker project</a>. This is where I found many great examples of property facets and relationships using Entity Framework.</p>
<p></holidayservices></iholidayservice></holidayevent></p>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; Code Only Looking Good (part 2)</title>
		<link>http://jasonrowe.com/2009/12/22/entity-framework-code-only-looking-good-part-2/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=entity-framework-code-only-looking-good-part-2</link>
		<comments>http://jasonrowe.com/2009/12/22/entity-framework-code-only-looking-good-part-2/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 05:19:53 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=855</guid>
		<description><![CDATA[Update: CTP3 is now available. This sample project below works fine with the new version but I have written a new post and sample project with some enhancements. In my initial post I tried out the &#8220;Code First&#8221; methodology of &#8230; <a href="http://jasonrowe.com/2009/12/22/entity-framework-code-only-looking-good-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><b>Update: CTP3 is now available. This sample project below works fine with the new version but I have written a new <a href="http://jasonrowe.com/2010/07/12/ef-ctp3-code-first-and-castle-windsor/">post and sample project</a> with some enhancements. </b></p>
<p>In my initial <a href="http://jasonrowe.com/2009/12/11/entity-framework-code-only/">post</a> I tried out the &#8220;Code First&#8221; methodology of Entity Framework. Taking the code only experiment a step further, I built a sample WCF service that queries and updates a table for holidays. You can browse and download the project via <a href="http://github.com/JasonRowe/EF-CTP2-HolidayService/archives/master">github</a>.</p>
<p>The console application &#8220;HolidayService.GenerateSchema&#8221; is very similar to the first post. It&#8217;s used to create the initial sqlexpress database and put in some initial test data. I did try out a simple configuration using some <a href="http://blogs.msdn.com/alexj/archive/2009/10/14/code-only-best-practices.aspx">best practices from this site</a>.
</p>
<p>The WCF service has two methods. </p>
<p><code><br />
    [ServiceContract]<br />
    public interface IHolidays<br />
    {<br />
        [OperationContract]<br />
        IList<holiday> GetHolidayDetails(DateTime startDate, DateTime endDate);</p>
<p>        [OperationContract]<br />
        int AddHolidayDetails(DateTime date, string name);<br />
    }<br />
</holiday></code></p>
<p>
In the HolidayService I created a HolidayManager class for CRUD operations. It uses a GlobalContext class to build an EF HolidayModel ObjectContext. For example, this is what that GetHolidays method looks like in the manager.
</p>
<p><code><br />
  using (holidayContext)<br />
  {<br />
     var holidayQueryResult = (from h in holidayContext.Holidays<br />
     where h.HolidayDate < endDate &#038; h.HolidayDate > startDate select h);</p>
<p>     foreach (var item in holidayQueryResult)<br />
     {<br />
          holidays.Add(item);<br />
     }<br />
  }<br />
</code></p>
<p>
In the GlobalContext class, is where my simple project started to feel some pain. It doesn&#8217;t feel right to be using a concrete version of the HolidayModel in the ContextBuilder which returns the ObjectContext. I also don&#8217;t like that the sql connection string and connection are all in this class. In another post, I&#8217;ll use some of the suggestions below to try to make this area more flexible.
</p>
<p><code><br />
var builder = new ContextBuilder<holidaymodel>();<br />
builder.Configurations.Add(new HolidayConfiguration());<br />
var connStr = ConfigurationManager.ConnectionStrings["UserDBConn"].ConnectionString;<br />
var connection = new SqlConnection(connStr);<br />
return builder.Create(connection);<br />
</holidaymodel></code></p>
<p>
Since this is a small project I didn&#8217;t go any further to clean up the GlobalContext class. I&#8217;ll leave that for next time I look into code only. Below are some links which investigate managing ObjectContext, dynamic models, and using EF in a business architecture.
</p>
<ol>
<li><a href="http://dotnetslackers.com/articles/ado_net/managing-entity-framework-objectcontext-lifespan-and-scope-in-n-layered-asp-net-applications.aspx">Managing Entity Framework ObjectContext</a></li>
<li><a href="http://blogs.msdn.com/alexj/archive/2009/11/09/tip-42-how-to-create-a-dynamic-model-using-code-only.aspx">how to create a dynamic model using code only</a></li>
<li><a href="http://daniel.wertheim.se/2009/11/13/entity-framework-4-part-4-autoregister-entitymappings/">Daniel Wertheim &#8211; EfEntityStore</a></li>
<li><a href="http://daniel.wertheim.se/2009/12/20/updates-to-putting-entity-framework-4-to-use-in-a-business-architecture/">Putting Entity framework 4 to use in a business architecture</a></li>
<li><a href="http://blogs.taiga.nl/martijn/tag/entity-framework/">Repository implementation &#8211; Entity Framework 4.0: a fresh start </a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2009/12/22/entity-framework-code-only-looking-good-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
