<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Blinking Caret</title>
	<atom:link href="http://blinkingcaret.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blinkingcaret.wordpress.com</link>
	<description></description>
	<lastBuildDate>Thu, 01 Dec 2011 21:44:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blinkingcaret.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Blinking Caret</title>
		<link>http://blinkingcaret.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blinkingcaret.wordpress.com/osd.xml" title="The Blinking Caret" />
	<atom:link rel='hub' href='http://blinkingcaret.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Getting the right encoding in a SSH session using PuTTY</title>
		<link>http://blinkingcaret.wordpress.com/2011/08/29/getting-the-right-encoding-in-a-ssh-session-using-putty/</link>
		<comments>http://blinkingcaret.wordpress.com/2011/08/29/getting-the-right-encoding-in-a-ssh-session-using-putty/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 22:06:31 +0000</pubDate>
		<dc:creator>ruidfigueiredo</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">https://blinkingcaret.wordpress.com/2011/08/29/getting-the-right-encoding-in-a-ssh-session-using-putty/</guid>
		<description><![CDATA[Recently I bought a cheap netbook to use as a media center at home, it came with Ubuntu 11.04. Everything worked fine out of the box (which is a major improvement for me compared with my previous experiences with Linux). I just installed XBMC and I was done. Recently I decided it would be nice [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=86&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I bought a cheap netbook to use as a media center at home, it came with Ubuntu 11.04. Everything worked fine out of the box (which is a major improvement for me compared with my previous experiences with Linux).</p>
<p>I just installed XBMC and I was done.</p>
<p>Recently I decided it would be nice if I could access it remotely (from Windows), so installed a ssh (openssh) server in it. It worked just fine, until I noticed that some applications would show strange characters, like if the text encoding was wrong, and that was what was happening, let me show you what I mean:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/08/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb.png?w=436&#038;h=276" width="436" height="276" /></a></p>
<p>That application is called mc (Midnight Commander) and it’s a nice text-based file explorer. Also I’m using <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">PuTTY</a> to connect to my Linux box. </p>
<p>First thing I had to do was to check which encoding was being used in my server. To do that in Linux just open a terminal and type locale. I’m pretty sure if you do this you’ll get UTF-8 as I did.</p>
<p>[If you want to read a little bit about encoding and why it is that you get strange characters when you are using the wrong encoding I recommend <a href="http://www.joelonsoftware.com/articles/Unicode.html">this blog post by Joel Spolsky</a>]</p>
<p>Now, to avoid the encoding problem you just have to configure PuTTY to use the right encoding so it can interpret those characters correctly. To do it just use the Window-&gt;Translation feature when configuring the connection: </p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/08/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb1.png?w=423&#038;h=407" width="423" height="407" /></a></p>
<p>And now, instead of all the â’s you get this:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/08/image2.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb2.png?w=438&#038;h=277" width="438" height="277" /></a></p>
<p>If you’re thinking about doing something like this, another useful piece of information is how to do it while having a dynamic ip (assigned to you by your ISP and liable to change at any time). I’m using No-IP for it, which worked just fine for me in Ubuntu, <a href="http://www.no-ip.com/downloads.php">here’s the link for it</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blinkingcaret.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blinkingcaret.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blinkingcaret.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=86&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blinkingcaret.wordpress.com/2011/08/29/getting-the-right-encoding-in-a-ssh-session-using-putty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/60425f4d0e7522cae73ac620cb1bea73?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ruidfigueiredo</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/08/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight Enabled WCF Services and Entity Framework 4 in a Silverlight Navigation Application</title>
		<link>http://blinkingcaret.wordpress.com/2011/05/09/silverlight-enabled-wcf-services-and-entity-framework-4-in-a-silverlight-navigation-application/</link>
		<comments>http://blinkingcaret.wordpress.com/2011/05/09/silverlight-enabled-wcf-services-and-entity-framework-4-in-a-silverlight-navigation-application/#comments</comments>
		<pubDate>Mon, 09 May 2011 15:05:00 +0000</pubDate>
		<dc:creator>ruidfigueiredo</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework 4]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">https://blinkingcaret.wordpress.com/2011/05/09/silverlight-enabled-wcf-services-and-entity-framework-4-in-a-silverlight-navigation-application/</guid>
		<description><![CDATA[If you want to create a Silverlight application that consumes “Silverlight enabled WCF services” that work with EF4 to return data here’s what you have to do: 1. Create a Silverlight Navigation Application (for example) 2.Add a ADO.Net Entity Data Model to the Web project (the one that will provide data to be returned by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=71&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you want to create a Silverlight application that consumes “Silverlight enabled WCF services” that work with EF4 to return data here’s what you have to do:</p>
<p>1. Create a Silverlight Navigation Application (for example)</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image7.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb7.png?w=459&#038;h=319" width="459" height="319" /></a></p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image8.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb8.png?w=336&#038;h=270" width="336" height="270" /></a></p>
<p>2.Add a ADO.Net Entity Data Model to the Web project (the one that will provide data to be returned by the methods the WCF service):</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image9.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb9.png?w=380&#038;h=264" width="380" height="264" /></a></p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image10.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb10.png?w=376&#038;h=336" width="376" height="336" /></a></p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image11.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb11.png?w=372&#038;h=332" width="372" height="332" /></a></p>
<p>Choose the tables you want the tool to include in your EF4 Conceptual model:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image12.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb12.png?w=341&#038;h=304" width="341" height="304" /></a></p>
<p>Regarding the ADO.Net Entity Data Model, you’re done.</p>
<p>3. The Silverlight enabled WCF service:</p>
<p>In the web project add a new Silverlight Enabled WCF service:</p>
<p><pre class="brush: csharp;">
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using UsingWCFServicesReloaded4.Web.Models;

namespace UsingWCFServicesReloaded4.Web.Services
{
    [ServiceContract(Namespace = &quot;&quot;)]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class LendingService
    {
        [OperationContract]
        public List&lt;Person&gt; GetPeople()
        {
            LendsEntitiesObjectContext context = new LendsEntitiesObjectContext();            
            return context.People.ToList&lt;Person&gt;();
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
</pre></p>
<p><strong>Do not fall in the temptation of prematurely disposing of the ObjectContext. </strong>You frequently see EF4 examples where the ObjectContext for your entities is created inside a <em>using</em> block, something like this:</p>
<p><pre class="brush: csharp;">
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using UsingWCFServicesReloaded4.Web.Models;

namespace UsingWCFServicesReloaded4.Web.Services
{
    [ServiceContract(Namespace = &quot;&quot;)]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class LendingService
    {
        [OperationContract]
        public List&lt;person&gt; GetPeople()
        {
            using (LendsEntitiesObjectContext context = new LendsEntitiesObjectContext())
            {
                return context.People.ToList&lt;person&gt;();
            }            
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
</pre></p>
<p>This will bring you pain, I assure you. The reason for this to subtly not work is that (unless you are using POCOs with EF4, and without the <em>virtual</em> <em>trick </em>[ask me about it, if you’re interested]) the entities that were auto generated for you will contain references to the ObjectContext, in this case Person has references to LendsEntitiesObjectContext which at the time of the WCF black magic that occurs between this method (<em>GetPeople</em>) being invoked (which involves serializing the list of persons so it can be sent through the wire) and the results getting to the client invoking it, tragedy will happen, because that LendsEntitiesObjectContext will not exist at that time.</p>
<p>Now you have to add this service to your silverlight app:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image13.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb13.png?w=365&#038;h=326" width="365" height="326" /></a></p>
<p>Click discover and your service should be there:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image14.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb14.png?w=393&#038;h=318" width="393" height="318" /></a></p>
<p>So there you go, there’s not much more to it than this, just for kicks here’s the XAML and the source code of the Home.xaml file that was autogenerated by the Silverlight Navigation Application Visual Studio template that I’ve changed to display a datagrid with the results returned from the GetPeople method of the WCF service:</p>
<p>Views\Home.xaml&#8217;s XAML:</p>
<p><pre class="brush: xml;">
&lt;navigation:Page x:Class=&quot;UsingWCFServicesReloaded4.Home&quot; 
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; 
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; 
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    xmlns:navigation=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation&quot;
    mc:Ignorable=&quot;d&quot; d:DesignWidth=&quot;640&quot; d:DesignHeight=&quot;480&quot;
    Title=&quot;Home&quot;
    Style=&quot;{StaticResource PageStyle}&quot; xmlns:sdk=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&quot;&gt;

    &lt;Grid x:Name=&quot;LayoutRoot&quot;&gt;
        &lt;ScrollViewer x:Name=&quot;PageScrollViewer&quot; Style=&quot;{StaticResource PageScrollViewerStyle}&quot;&gt;

            &lt;StackPanel x:Name=&quot;ContentStackPanel&quot;&gt;

                &lt;TextBlock x:Name=&quot;HeaderText&quot; Style=&quot;{StaticResource HeaderTextStyle}&quot; 
                                   Text=&quot;Home&quot;/&gt;
                &lt;TextBlock x:Name=&quot;ContentText&quot; Style=&quot;{StaticResource ContentTextStyle}&quot; 
                                   Text=&quot;Home page content&quot;/&gt;
                &lt;sdk:DataGrid AutoGenerateColumns=&quot;True&quot; 
                              Name=&quot;PeopleDataGrid&quot;/&gt;
                &lt;Button x:Name=&quot;RefreshButton&quot; Click=&quot;RefreshButton_Click&quot;&gt;
                    Refreh
                &lt;/Button&gt;
            &lt;/StackPanel&gt;

        &lt;/ScrollViewer&gt;
    &lt;/Grid&gt;
&lt;/navigation:Page&gt;
</pre></p>
<p>Code Behind:</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UsingWCFServicesReloaded4
{
    public partial class Home : Page
    {
        LendingService.LendingServiceClient _proxy = new LendingService.LendingServiceClient();
        public Home()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _proxy.GetPeopleCompleted += new EventHandler&lt;LendingService.GetPeopleCompletedEventArgs&gt;(_proxy_GetPeopleCompleted);
        }

        void _proxy_GetPeopleCompleted(object sender, LendingService.GetPeopleCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                PeopleDataGrid.ItemsSource = e.Result;                
            }
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            _proxy.GetPeopleAsync();
        }
    }
}
</pre><br />
Here&#8217;s a snapshot of what you should get when you hit refresh: </p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image15.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb15.png?w=413&#038;h=234" width="413" height="234" /></a></p>
<p>And just in case you want to try it with the simple database I’ve used here’s the DDL for it:</p>
<p><pre class="brush: sql;">

-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/09/2011 16:03:32
-- Generated from EDMX file: ...Lends.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Lends];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Valuable_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Valuables] DROP CONSTRAINT [FK_Valuable_Person];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[People]', 'U') IS NOT NULL
    DROP TABLE [dbo].[People];
GO
IF OBJECT_ID(N'[dbo].[Valuables]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Valuables];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'People'
CREATE TABLE [dbo].[People] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(50)  NOT NULL,
    [LastName] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Valuables'
CREATE TABLE [dbo].[Valuables] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [HolderID] int  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'People'
ALTER TABLE [dbo].[People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Valuables'
ALTER TABLE [dbo].[Valuables]
ADD CONSTRAINT [PK_Valuables]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [HolderID] in table 'Valuables'
ALTER TABLE [dbo].[Valuables]
ADD CONSTRAINT [FK_Valuable_Person]
    FOREIGN KEY ([HolderID])
    REFERENCES [dbo].[People]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Valuable_Person'
CREATE INDEX [IX_FK_Valuable_Person]
ON [dbo].[Valuables]
    ([HolderID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blinkingcaret.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blinkingcaret.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blinkingcaret.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=71&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blinkingcaret.wordpress.com/2011/05/09/silverlight-enabled-wcf-services-and-entity-framework-4-in-a-silverlight-navigation-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/60425f4d0e7522cae73ac620cb1bea73?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ruidfigueiredo</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb11.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb15.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4&#8211; Using POCOs</title>
		<link>http://blinkingcaret.wordpress.com/2011/05/06/entity-framework-4-using-pocos/</link>
		<comments>http://blinkingcaret.wordpress.com/2011/05/06/entity-framework-4-using-pocos/#comments</comments>
		<pubDate>Fri, 06 May 2011 14:12:59 +0000</pubDate>
		<dc:creator>ruidfigueiredo</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Entity Framework 4]]></category>
		<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">https://blinkingcaret.wordpress.com/2011/05/06/entity-framework-4-using-pocos/</guid>
		<description><![CDATA[Recently I decided to have a look at the Entity Framework 4. I had a look at the Firestarter videos (some of them at least) and liked particularly of the idea of being able to use your own Model classes together with Entity Framework. These are what they refer to as POCOs or Plain Old [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=36&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I decided to have a look at the Entity Framework 4. I had a look at the <a href="http://channel9.msdn.com/posts/Entity-Framework-Firestarter-Session-1-of-6">Firestarter</a> videos (some of them at least) and liked particularly of the idea of being able to use your own Model classes together with Entity Framework. These are what they refer to as POCOs or Plain Old CLR Objects.The idea is that instead of having the requirement of your classes having to inherit from some other class so that they get the functionality necessary for them to work with the framework (in this case EF4), you just follow conventions, mostly naming conventions, like your classes having to have the same name as the entities in the conceptual model and what not, and then the framework will use reflection at runtime to get the information it needs to make it all work.</p>
<p>If your new to Entity Framework this will definitely sound alien, but fear not, I’ll shortly describe what I think is the minimum information necessary to make sense of it all.</p>
<p>First, what is Entity Framework, what’s its purpose?</p>
<p>It is an ORM (object relational mapper), which aims at providing an automated way of getting your relational data (the data you have stored in the database) to objects you use in your code, and vice-versa. And to achieve that you need to have a mapping that describes how the properties in your objects relate to the particular columns in the corresponding tables in your database. For example you have a Person class in your code with two strings, FirstName and LastName, and a table also named Person with two nvarchar columns with Name, and Surname. Your mapping will describe that FirstName should be “synched” with Name and LastName with “Surname”.</p>
<p>That’s what ORM’s are about. The nice thing about EF, is that you have the notion of Entity. In EF the mapping is between the Entities and the Tables in the database, which is nice because there are situations where it might be better for you if your model classes aren’t a perfect reflection of your database tables. (As an exercise think how you’d represent a many-to-many relationship in a relational database and how you’d represent it in an OO language). And the reason why this is so nice will become more obvious when you know how to use your own POCOs, but that’s later in the text.</p>
<p>So in EF, there are entities and a mapping between them and the DB. Entities can have S<em>calar </em>properties (which you can think of normal properties, like a Name string) and <em>Navigation</em> properties, which represent relationships between entities. For example if you have an Entity that represents a Book, and that Book has an Owner (which is represented by an entity Person), Owner would be a Navigation property (btw, in the DB this would be represented by having the Book table have a OwnerID column which is a Foreign Key to Person). Navigation properties can have different <em>multiplicities: 0..1, 1, or * </em>(which means many; also probably 1..* but I’m feeling too lazy to check).</p>
<p>The entities and their relationships (that are translated to navigation properties) are often referred to as <em>Conceptual Model</em>, but I’ve heard people call it Object model too, but I’m pretty sure conceptual model is the right term.</p>
<p>The really nice thing about EF is that in Visual Studio 2010 you can auto generate the model from the database  metadata. You can also do it the other way around: create a conceptual model and then ask for the database schema to be generated (although this option is kind of hidden, ask me about it if you are curious about it).</p>
<p>If you want to have a look at how the Conceptual Model and Mappings are represented just open the EF’s edmx files in a XML editor and have a look.</p>
<p>The last important concept is the <em>ObjectContext</em>. To understand its importance bear with me through this imaginary scenario.</p>
<p>You get a collection of Books (that were gotten for you by EF [if you’re wondering how, don’t, it’s not important for you to get this part]), you decide to change the Title of one of them, you do it… and now what? You have the mappings and all, but how will that be (automatically) translated into an Update to your DB?</p>
<p>It’s the ObjectContext’s responsibility to do that, to figure out that the entity was changed and that that should be translated into an Update to the corresponding row in the Books table in the database. [This is a ridiculously simplistic description, but I think it’s sufficient]</p>
<p>The way ObjectContext does this varies if you use the auto generate feature or if you use POCOs. If you use the auto generate feature (this is the default) you get classes that represent the entities in your conceptual model and that inherit from EntityObject. These classes have logic that notify the ObjectContext of the changes that are done to them. If you use POCOs the way that change tracking works is by storing a copy of your object and when you decide to commit your changes the ObjectContext will compare the new version of your objects with the one it had when they were created (the metaphor is of a Snapshot that is taken at creation time and at commit time, you compare both and then do the right thing so that the DB gets “synched” with your objects).</p>
<p>Ok, now for the reason of this post: how to use your own model classes (POCOs) and Entity Framework. Baby step by baby step.</p>
<p>Step One: Get a database to try this, here’s the DDL for the one I used in this simple example, just open <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=08e52ac2-1d62-45f6-9a4a-4b76a8564a2b">SQL Management Studio</a> and paste it to a new query (or just use your favorite way of creating the database). [I’m using SQLServer 2008 Express, and Management Studio 2008 Express]</p>
<p>The database has two tables: Person and Valuable (the idea is that you can store People and Valuables and who holds those valuables, so 2 tables, and a foreign key):</p>
<p><strong>Table Person:</strong></p>
<p>ID, FirstName, LastName</p>
<p>ID is primary Key</p>
<p><strong>Table Valuable:</strong></p>
<p>ID, Description, OwnerID</p>
<p>ID is primary key</p>
<p>OwnerID is Foreign Key to Person.ID (can be null)</p>
<p>The DDL:</p>
<p><pre class="brush: sql;">
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/06/2011 15:27:10
-- Generated from EDMX file: ...\Lends.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Lends];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Valuable_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Valuables] DROP CONSTRAINT [FK_Valuable_Person];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[People]', 'U') IS NOT NULL
    DROP TABLE [dbo].[People];
GO
IF OBJECT_ID(N'[dbo].[Valuables]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Valuables];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'People'
CREATE TABLE [dbo].[People] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(50)  NOT NULL,
    [LastName] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Valuables'
CREATE TABLE [dbo].[Valuables] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [HolderID] int  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'People'
ALTER TABLE [dbo].[People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Valuables'
ALTER TABLE [dbo].[Valuables]
ADD CONSTRAINT [PK_Valuables]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [HolderID] in table 'Valuables'
ALTER TABLE [dbo].[Valuables]
ADD CONSTRAINT [FK_Valuable_Person1]
    FOREIGN KEY ([HolderID])
    REFERENCES [dbo].[People]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Valuable_Person1'
CREATE INDEX [IX_FK_Valuable_Person1]
ON [dbo].[Valuables]
    ([HolderID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
</pre></p>
<p>Step Two: Open Visual Studio 2010, File-&gt;New Project-&gt;Console Application (because we want to keep it simple).</p>
<p>Step Three: Add the EF4 model:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb.png?w=525&#038;h=364" alt="image" width="525" height="364" border="0" /></a></p>
<p>(I named it Lends)</p>
<p>Click Add:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb1.png?w=366&#038;h=327" alt="image" width="366" height="327" border="0" /></a></p>
<p>Generate from database, click Next:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image2.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb2.png?w=366&#038;h=327" alt="image" width="366" height="327" border="0" /></a></p>
<p>Choose the appropriate data connection for the database you created in step one, change the name of the connection string if you want (I left the default: LendsEnitites), click Next:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image3.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb3.png?w=372&#038;h=332" alt="image" width="372" height="332" border="0" /></a></p>
<p>Select the tables, and if you are from a non-speaking English country like me, you don’t get the “Pluralize or singularize generated object names” selected by default. Because our DB has English names select it. This is a nice feature because it names the EntitySets using the plural form of the Entity names. For example, the entity Person will be stored in an EntitySet named People (yes, People, its smart, it didn’t name it Persons, although I think that’s also correct). It’s just a nice feature, don’t worry too much about it.</p>
<p>Click Finish:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb4.png?w=368&#038;h=291" alt="image" width="368" height="291" border="0" /></a></p>
<p>Here’s the view of your conceptual model. The rounded boxes are the entities, the Properties are the <em>Scalar</em> properties and the <em>Navigation </em>properties are … well the Navigation properties. The line between the two entities is the Relationship that defines the navigation properties.</p>
<p>I’m going to change the name of the Valuable navigation property from Person to Holder, because that makes more sense to me (and just because I can, and to illustrate that has an effect on your POCO class):</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image5.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb5.png?w=363&#038;h=287" alt="image" width="363" height="287" border="0" /></a></p>
<p>Now I’m going to turn off the auto generate feature (which is what happens by default, you get classes that represent your entities and that inherit from EntityObject) because this post is not about that, it is about using your classes (not generated code) as the model.</p>
<p><strong>Be sure that the designer window is selected</strong>, to do this click anywhere in it, and then go to properties and change the “Code Generation Strategy” from <strong>Default</strong> to <strong>None</strong>:</p>
<p><a href="http://blinkingcaret.files.wordpress.com/2011/05/image6.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb6.png?w=413&#038;h=246" alt="image" width="413" height="246" border="0" /></a></p>
<p>Now we just have to create the class that will be our ObjectContext and our model classes (the POCOs).</p>
<p>There are some things you have to know about the class that will have the role of being the ObjectContext:</p>
<ol>
<li>It has to inherit from ObjectContext (the namespace is System.Data.Objects)</li>
<li>It has to call base to initialize in the constructor, I recommend you use this overload of the base constructor: base(“name=ConnectionStringName”, “EntityContainerName”)</li>
<ol>
<li>You can check the name of the connection string at App.config, and the EntityContainerName in the conceptual model properties in the designer</li>
<li>In the constructor turn on LazyLoading (LazyLoading just makes it so that when you use the object context to get, for example a Person, it doesn’t automatically get all the Valuables that are associated with that Person, I didn’t try it with it off, so bear with me, and usually you’ll want to use this for performance reasons anyway)</li>
</ol>
<li>It has to have a ObjectSet&lt;T&gt; property for each type of entity, where T is your POCO class</li>
</ol>
<p>Here’s the class:<br />
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace UsingEF4WithPOCOsReloaded
{
    class LendsObjectContext : ObjectContext
    {
        public LendsObjectContext()
            : base(&quot;name=LendsEntities&quot;, &quot;LendsEntities&quot;)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }

        private ObjectSet _people;
        public ObjectSet People
        {
            get
            {
                return _people ?? (_people = CreateObjectSet());
            }
        }

        private ObjectSet _valuables;
        public ObjectSet Valuables
        {
            get
            {
                return _valuables ?? (_valuables = CreateObjectSet());
            }
        }
    }
}
</pre></p>
<p>Just in case you don’t know, the ?? operator returns the first non-null value.</p>
<p>Now our model classes themselves:</p>
<p>Some notes:</p>
<ol>
<li>They have to have the same name as the corresponding entities in the conceptual model</li>
<li>The have to have properties with <strong>exactly</strong> the same name as the <em>scalar </em>properties of the corresponding entity in the conceptual model
<ol>
<li>Don’t mess this up, you’ll only get notified (via exceptions) at runtime</li>
</ol>
</li>
<li>Have ICollection&lt;T&gt; properties for the relations that have more than multiplicity of one (for example, in our case a Person can have several Valuables associated with him/her)</li>
<li>Have the corresponding POCO property for 0/0..1/1 multiplicity relationships (for example, a valuable has only one Holder).</li>
</ol>
<p>Here are the POCO classes for the Person and Valuable:</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingEF4WithPOCOsReloaded
{
    public class Person
    {
        public Person()
        {
            Valuables = new List();
        }

        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection Valuables { get; set; }
    }
}
</pre></p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingEF4WithPOCOsReloaded
{
    public class Valuable
    {
        public int ID { get; set; }
        public string Description { get; set; }
        public int HolderID { get; set; }
        public Person Holder { get; set; }
    }
}
</pre></p>
<p><strong>Always double check the name of the properties, they have to be exactly the same as in the conceptual model</strong>.</p>
<p>And finally here’s my main class, it lists all People and their valuables, and ask you to to insert the data for a person and a valuable. If that person already exists that valuable is associated with him/her, and if that valuable already exists it gets associated with the person you entered:</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingEF4WithPOCOsReloaded
{
    class Program
    {
        static void Main(string[] args)
        {
            using (LendsObjectContext context = new LendsObjectContext())
            {
                foreach (Person person in context.People)
                {
                    Console.WriteLine(&quot;Person: {0}, {1} holds the following items: &quot;, person.LastName, person.FirstName);
                    //Handling Lazy Loading: if you don't do this with 
                    //Lazy Loading enabled, the Valuables collections (that is linked by EF to 
                    //a navigation property) would be empty
                    context.LoadProperty(person, p =&gt; p.Valuables);
                    if (person.Valuables.Count == 0)
                    {
                        Console.WriteLine(&quot; None&quot;);
                    }
                    else
                    {
                        foreach (Valuable valuable in person.Valuables)
                        {
                            Console.WriteLine(&quot; -{0}&quot;, valuable.Description);
                        }
                    }
                    //By the way, this scenario is what is described as the N+1 problem when performance is mentioned in the context of ORMs
                    //it's N+1 because you have 1 query for Person and N queries for the Valuables
                    //The best way to deal with this is to use Stored Procedures (not mentioned in this post)
                }

                Console.WriteLine();
                Console.WriteLine(&quot;Add a new Person/Valuable:&quot;);
                Console.Write(&quot;First Name: &quot;);
                string firstName = Console.ReadLine();
                Console.Write(&quot;Last Name: &quot;);
                string lastName = Console.ReadLine();
                Console.Write(&quot;Valuable's Description: &quot;);
                string valuableDescription = Console.ReadLine();

                var personQuery = from p in context.People
                                  where (p.FirstName == firstName &amp;&amp; p.LastName == lastName)
                                  select p;

                Person holder;
                if (personQuery.Count() == 0)
                {
                    holder = new Person();
                    holder.FirstName = firstName;
                    holder.LastName = lastName;
                    context.People.AddObject(holder);
                }
                else
                {
                    holder = personQuery.First();
                }

                var valuableQuery = from v in context.Valuables
                                    where v.Description == valuableDescription
                                    select v;
                Valuable item;
                if (valuableQuery.Count() == 0)
                {
                    item = new Valuable();
                    item.Description = valuableDescription;
                    context.Valuables.AddObject(item);
                }
                else
                {
                    item = valuableQuery.First();
                }

                item.Holder = holder;                
                context.SaveChanges();
            }


        }        
    }
}
</pre><br />
<strong>Notes:</strong> Notice that when you create a new Person or Valuable you have to add it to the context, so that it knows how they were when they were created and subsequently be able to compare that version with the one that exists when SaveChanges is called.</p>
<p><strong>Disclaimer</strong>: this code is an insult and a mockery of best practices, it is only to illustrate how to use EF4 with POCOs, you really shouldn’t copy&amp;paste it and use it in a production environment.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blinkingcaret.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blinkingcaret.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blinkingcaret.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=36&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blinkingcaret.wordpress.com/2011/05/06/entity-framework-4-using-pocos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/60425f4d0e7522cae73ac620cb1bea73?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ruidfigueiredo</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://blinkingcaret.files.wordpress.com/2011/05/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>How Hard Can it Be?</title>
		<link>http://blinkingcaret.wordpress.com/2010/10/19/how-hard-can-it-be/</link>
		<comments>http://blinkingcaret.wordpress.com/2010/10/19/how-hard-can-it-be/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 00:55:42 +0000</pubDate>
		<dc:creator>ruidfigueiredo</dc:creator>
				<category><![CDATA[ClassicASP]]></category>

		<guid isPermaLink="false">http://blinkingcaret.wordpress.com/?p=93</guid>
		<description><![CDATA[Recently a friend of mine asked me for help in one of his projects. He is managing a site developed using ASP, he did not start it, he just kind of inherited it. Not ASP.Net though, classical ASP. So, imagine this, you are a more or less experienced developer, but you have never developed in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=93&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently a friend of mine asked me for help in one of his projects. He is managing a site developed using ASP, he did not start it, he just kind of inherited it. Not ASP.Net though, classical ASP.</p>
<p>So, imagine this, you are a more or less experienced developer, but you have never developed in ASP. Someone asks you to lend a hand in an ASP project. You think, I feel pretty confident about doing web development, how hard can it be? Well, knowing this now, I can say this: harder than it should.</p>
<p>If you had to enumerate what you had to do before you starting something like this, it would probably look a bit like this:</p>
<ol>
<li>Install a web server that can serve ASP pages. Internet Information Server (IIS) is really the only choice here.</li>
<li>Install ASP in IIS. IIS does not support ASP out of the box, you have to install it</li>
<li>Get a copy of the source code and put it running on your local machine</li>
<li>Start developing</li>
</ol>
<p>Sounds pretty easy huh? Well it should, however for me it was not (it never is, disagree? read <a href="http://www.shamusyoung.com/twentysidedtale/?p=9557">this</a>).</p>
<p>Let us start by (1). Installing IIS. The preferred way to get IIS running in your machine nowadays is to use <a href="http://www.microsoft.com/web/downloads/platform.aspx" target="_blank">Microsoft Web Platform Installer</a> (WPI). If you do not know what I am talking about, check it out, it is really nice (if it is works in your machine).</p>
<p>By the way, I am using a Windows 7 Professional 64Bit.</p>
<p>Here is what happened, I would start WPI and choose Web Platform and Web Server. Actually I chose the customize option and realized I could install ASP from WPI, so I would get 1 &amp; 2 at the same time. At that time I thought, man this WPI is really nice.</p>
<p><a href="http://properdev.files.wordpress.com/2010/10/wpi.png"><img class="aligncenter size-full wp-image-6" title="Installing a Web Server with WPI" src="http://properdev.files.wordpress.com/2010/10/wpi.png?w=630" alt="Installing a Web Server"   /></a></p>
<p>However&#8230; as soon I clicked the Install button&#8230; tragedy happened&#8230;</p>
<p>It would not install a thing, everything would throw an error. I do not actually recall the exact error, or what was in the log (update: I had this in my browser search history: web platform installer Unattended setup returned &#8216;-2147467259&#8242;; and this one: web platform installer download manager error), I do know that I eventually found out why it was throwing all those errors, my TrustedInstaller.exe was missing. The process of finding out this was happening because TrustedInstaller was missing was not obvious at all. Making a long story short, I discovered it was missing when I tried to do run SFC (System file checker) and got another error (sorry this one I did not really wrote down, but it is easy to provoke if you really want to, just delete TrustedInstaller and then try to run SFC <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )</p>
<p>Oh, and just another symptom that a missing TrustedInstaller will cause: if you go to Control Panel-&gt;Programs-&gt;Turn Windows features On Off you&#8217;ll get an empty list.</p>
<p>By the way, you should have your TrustedInstaller.exe file in C:\Windows\servicing.</p>
<p>I still do not know for sure how it got deleted, but it turns out that <a href="http://social.technet.microsoft.com/Forums/en-US/itprovistasetup/thread/4e9b31de-5628-4083-9e3a-cd9e0d1aeeac">Avast antivirus might sometimes delete it</a>. I do use Avast so, that might have been it&#8230;</p>
<p>So now what? You have a <a href="http://en.wikipedia.org/wiki/Windows_Resource_Protection">pretty important system file missing</a>, what do you do? Some people would just go about formatting their pc and reinstalling everything they need. Some are a bit more organized and they keep a clean image file of their system that they have created after a fresh installation.</p>
<p>I am neither of these people. I do not keep an image of a fresh install nor am I willing to format my PC every time something like this happens.</p>
<p>I do not know if you know this but you can access individual files from your Windows installation disk. Here&#8217;s how you do it: download <a href="http://www.7-zip.org/">7-Zip file manager</a>, then insert your Windows DVD and browse to sources/install.wim. Use 7-Zip to open it.</p>
<p>Inside install.wim you will find a bunch of folders and a file named 1.xml. I&#8217;d advise you to open it in Internet Explorer if you do not have any program that can automatically indent it for you (it has no line breaks). Look for the &lt;Image Index=&#8221;x&#8221;&gt; tag that contains a child element named &#8216;NAME&#8217; that corresponds to your version of Windows. In my case it was &lt;NAME&gt;Windows 7 PROFESSIONAL&lt;/NAME&gt; inside &lt;IMAGE INDEX=&#8221;<strong>3</strong>&#8220;&gt; meaning that the files for my installation are under the folder named 3.</p>
<p>Now you just have to browse through your corresponding folder and extract the file you need. In this case TrustedInstaller.exe under Windows\servicing.</p>
<p>If you have never done something like this before, you will be surprised if you try to copy that file to your installation of Windows. You will be presented with a &#8220;Permission denied&#8221; error message. You have to take ownership of that folder (using takeown) and give permissions to it before you can copy the file there.</p>
<p>Ok! After all this I was able to install IIS 7.5 and ASP using the WPI! What should have happened here after the install had finished, is me creating a simple ASP file, putting it under C:\inetput\wwwroot\ and then opening http://localhost/helloworld.asp and seeing magic happen. This did not work&#8230;</p>
<p>Although I had used the customize option in WPI and chosen to install IIS and ASP apparently, regarding ASP, nothing happened. I do not actually know for sure if ASP was installed by WPI or not, but I know that it was not ready to be used in IIS. To make it work you have to go to Control Panel-&gt;Programs-&gt;Turn Windows Features On Off and choose: Internet Information Services-&gt;World Wide Web Services-&gt;Application Development Features-&gt;ASP.</p>
<p>Now helloworld.asp works!</p>
<p>For making the real project work copy it to C:\Inetput\wwwroot\YourProject. Now, I am not entirely sure, but I think it did not work at that time, you have to open a console as an administrator and takeown of C:\Inetput\wwwroot\YourProject like this: takeown /f C:\Inetput\wwwroot\YourProject -r. And then edit the permissions using Explorer by right-clicking in C:\Inetput\wwwroot\YourProject, and then give permissions to your user and IIS_USRS.</p>
<p>The project my friend sent me had some errors, but I could not see much when I tried to run it. What I got from the browser was a &#8220;friendly&#8221; error message that hid the error details from me. Actually IIS does not provide that information at all. To get ASP errors to be displayed in Internet Explorer you have to configure IIS to display them: Open IIS manager, go to ASP-&gt;Compilations-&gt;Debugging Properties and enable Scrip Error Message.</p>
<p>Then disable Friendly Error Messages in IE by going to Tools-&gt;Internet Options-&gt;Browsing and then disable Show Friendly HTTP Error Messages.</p>
<p>But the story does not end here&#8230; The site was really old and was accessing a Microsoft Access Database. When I tried to run it I got this message:</p>
<p><span style="font-size:x-small;">Microsoft OLE DB Provider for ODBC Drivers</span> <span style="font-size:x-small;">error &#8217;80004005&#8242;</span></p>
<p><span style="font-size:x-small;">[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified</span></p>
<p>Making a long story short (again!), <a href="http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/dfbd0406-bc69-436e-998b-7fc454012f91">there aren&#8217;t any 64-Bit drivers for Access</a>. You can try to install <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d">this</a>, but beware, I installed the 32 bit version by mistake and then I could not install the 64 bit version. I tried uninstalling the 32 bit installation and then installing the 64 bit one, just to get a message from the 64 bit version prompting me to uninstall the 32 bit version that I had already uninstalled!</p>
<p>But really, the easiest way is not that one (you probably have to have Access installed for this to work. I did, so I cannot really tell if this would work otherwise). Change IIS&#8217;s Application Pool to run in 32 Bit mode. To do this open IIS manager, then go to Application Pools; to your relevant application pool, select it and in the right pane select Advanced Settings&#8230; and in (General) set Enable 32-Bit Applications to True. And now you are done! At least I was.</p>
<p>So&#8230; how hard can it be? Harder than I had imagined, but then again nothing that should strike you as being out of the ordinary if you have done this kind of thing a couple of times. The truth is, it really rarely works at the first try.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blinkingcaret.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blinkingcaret.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blinkingcaret.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blinkingcaret.wordpress.com&amp;blog=19632893&amp;post=93&amp;subd=blinkingcaret&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blinkingcaret.wordpress.com/2010/10/19/how-hard-can-it-be/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/60425f4d0e7522cae73ac620cb1bea73?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ruidfigueiredo</media:title>
		</media:content>

		<media:content url="http://properdev.files.wordpress.com/2010/10/wpi.png" medium="image">
			<media:title type="html">Installing a Web Server with WPI</media:title>
		</media:content>
	</item>
	</channel>
</rss>
