<?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>.Net and MOSS News</title>
	<atom:link href="http://nadege.deroussen.net/feed" rel="self" type="application/rss+xml" />
	<link>http://nadege.deroussen.net</link>
	<description>.Net and Sharepoint informations, tips and tricks</description>
	<lastBuildDate>Sun, 20 May 2012 15:42:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Error while trying to connect to Team Foundation Server 2011 Express</title>
		<link>http://nadege.deroussen.net/error-while-trying-to-connect-to-team-foundation-server-2011-express</link>
		<comments>http://nadege.deroussen.net/error-while-trying-to-connect-to-team-foundation-server-2011-express#comments</comments>
		<pubDate>Sun, 20 May 2012 14:08:28 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[TFS]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Express]]></category>
		<category><![CDATA[TF246017]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1294</guid>
		<description><![CDATA[I have just installed TFS Express 2011 Beta and try to use it. First thing I do is create a new Team Project to add some source code. It worked perfectly. Then I try to add a new user to my project. TFS Express should allow until 5 users. I create a user on my [...]]]></description>
			<content:encoded><![CDATA[<p>I have just installed TFS Express 2011 Beta and try to use it. First thing I do is create a new Team Project to add some source code. It worked perfectly. Then I try to add a new user to my project. TFS Express should allow until 5 users. I create a user on my machine and, using Visual Studio 2011, I add this user as Contributor in my project. Once done, I try to connect to the Web interface using http://<myserver>:8080/tfs. I try to connect using the new user credentials but I get an error :<br />
<strong>TF246017: Team Foundation Server could not connect to the database. Verify that the server that is hosting the database is operational, and that network problems are not blocking communication with the server.</strong><br />
<a href="http://nadege.deroussen.net/wp-content/uploads/2012/05/TFS246017.png"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/05/TFS246017-300x158.png" alt="" title="TFS246017" width="300" height="158" class="aligncenter size-medium wp-image-1295" /></a></p>
<p>After some research, I finally found what is the problem.<br />
Looking at the EventViewer, I found this message in the application logs :<br />
Login failed for user <myuser>. Reason: Failed to open the explicitly specified database &#8216;Tfs_Configuration&#8217;.</p>
<p>For some reason, my user tries to access the configuration database. Why ?<br />
Because of a misconfiguration in IIS. ASP.Net Impersonation is enabled whereas it should not. I disable it and reload the page. It works now.</p>
<p>If you get this error, hope this post will help you.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/error-while-trying-to-connect-to-team-foundation-server-2011-express/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[EF] Code Fluent Configuration file</title>
		<link>http://nadege.deroussen.net/ef-code-fluent-configuration-file</link>
		<comments>http://nadege.deroussen.net/ef-code-fluent-configuration-file#comments</comments>
		<pubDate>Mon, 16 Apr 2012 18:05:08 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[EF]]></category>
		<category><![CDATA[O/R Mapper]]></category>
		<category><![CDATA[Code Fluent]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[EF CodeFirst]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1255</guid>
		<description><![CDATA[In my previous post, we saw how to configure Code First database generation with Code Fluent. Here is what we&#8217;ve done in the previous post : public class MyContext : DbContext { public DbSet&#60;Person&#62; Persons { get; set; } public DbSet&#60;Project&#62; Projects { get; set; } public DbSet&#60;Task&#62; Tasks { get; set; } public MyContext():base("EFSample") [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, we saw how to configure Code First database generation with Code Fluent.</p>
<p>Here is what we&#8217;ve done in the previous post :</p>
<pre class="brush:csharp">public class MyContext : DbContext
    {
        public DbSet&lt;Person&gt; Persons { get; set; }
        public DbSet&lt;Project&gt; Projects { get; set; }
        public DbSet&lt;Task&gt; Tasks { get; set; } 

        public MyContext():base("EFSample")
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity&lt;Task&gt;().HasRequired(t =&gt; t.Project).WithMany(p =&gt; p.Tasks).HasForeignKey(t =&gt; t.ProjectId).WillCascadeOnDelete(false);
            modelBuilder.Entity&lt;Task&gt;().HasRequired(p =&gt; p.AssignedTo);
            modelBuilder.Entity&lt;Person&gt;().Property(d =&gt; d.BirthDate).HasColumnName("Birth");
            modelBuilder.Entity&lt;Project&gt;().Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();
            modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);
        }
    }</pre>
<p>As you can see, in real project, you can fastly get hundred of configuration lines here and it can become difficult to browse and to maintain. That&#8217;s why, Code First allows you to use configuration files to manage your configuration by entity type.<br />
<span id="more-1255"></span><br />
First, we will create a new class to contain our configuration for the Task entity. reate a new class named <strong>TaskConfiguration</strong> and let it inherit from EntityTypeConfiguration (You should add the following namespace : <em>System.Data.Entity.ModelConfiguration</em>). Add a default constructor to this class. When this is done, move our configuration from MyContext class to TaskConfiguration. Here is how to modify your class :</p>
<pre class="brush:csharp">public class TaskConfiguration : EntityTypeConfiguration&lt;Task&gt;
    {
        public TaskConfiguration()
        {
            Property(t =&gt; t.Title).IsRequired().HasMaxLength(255);
            HasRequired(t =&gt; t.AssignedTo);
            HasRequired(t =&gt; t.Project).WithMany(p =&gt; p.Tasks).HasForeignKey(t =&gt; t.ProjectId).WillCascadeOnDelete(false);
        }
    }</pre>
<p>You can see I&#8217;ve added a new constraint about the Name length and I have just remove the <em>modelBuilder.Entity<T>()</em>. Easy, no ?!</p>
<p>On the MyContext class, be sure you&#8217;ve deleted the line about the Task class and add the following line to register the configuration class for Task :</p>
<pre class="brush:csharp">modelBuilder.Configurations.Add(new TaskConfiguration());</pre>
<p>Here is what the MyContext class should look like :</p>
<pre class="brush:csharp">public class MyContext : DbContext
    {
        public DbSet&lt;Person&gt; Persons { get; set; }
        public DbSet&lt;Project&gt; Projects { get; set; }
        public DbSet&lt;Task&gt; Tasks { get; set; } 

        public MyContext():base("EFSample")
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new TaskConfiguration());
            modelBuilder.Entity&lt;Person&gt;().Property(d =&gt; d.BirthDate).HasColumnName("Birth");
            modelBuilder.Entity&lt;Project&gt;().Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();
            modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);
        }
    }</pre>
<p>Now, you can run the application and check your database. This is the expected result :</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/TaskConfiguration.png"><img class="aligncenter size-full wp-image-1265" title="TaskConfiguration" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/TaskConfiguration.png" alt="" width="259" height="128" /></a></p>
<p>So, now, let&#8217;s do the same for Project and Person configuration.</p>
<pre class="brush:csharp">public class ProjectConfiguration : EntityTypeConfiguration&lt;Project&gt;
    {
        public ProjectConfiguration()
        {
            Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();
            HasRequired(p =&gt; p.Manager);
        }
    }</pre>
<pre class="brush:csharp">public class PersonConfiguration : EntityTypeConfiguration&lt;Person&gt;
    {
        public PersonConfiguration()
        {
            Property(d =&gt; d.BirthDate).HasColumnName("Birth");
        }
    }</pre>
<p>And here is our new MyContext class. It now contains only &#8220;links&#8221; to our configuration classes :</p>
<pre class="brush:csharp">public class MyContext : DbContext
    {
        public DbSet&lt;Person&gt; Persons { get; set; }
        public DbSet&lt;Project&gt; Projects { get; set; }
        public DbSet&lt;Task&gt; Tasks { get; set; } 

        public MyContext():base("EFSample")
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new TaskConfiguration());
            modelBuilder.Configurations.Add(new ProjectConfiguration());
            modelBuilder.Configurations.Add(new PersonConfiguration());
        }
    }</pre>
<p>If you run the application, now, the database would be created exactly as it was before and now, it would be easier to locate the code to modify in order to configure our database tables.</p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/ef-code-fluent-configuration-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[EF] Tips : Change database name created by Code First</title>
		<link>http://nadege.deroussen.net/ef-tips-change-database-name-created-by-code-first</link>
		<comments>http://nadege.deroussen.net/ef-tips-change-database-name-created-by-code-first#comments</comments>
		<pubDate>Mon, 16 Apr 2012 16:15:38 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[EF]]></category>
		<category><![CDATA[O/R Mapper]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[EF CodeFirst]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1249</guid>
		<description><![CDATA[If you are trying to use Code First, you should have seen the database name used by default is not always very useful and you surely want to rename it to use something easier. To do that, you simply have to modified your Context class ti use the name you&#8217;ve choosen. Here is a simple [...]]]></description>
			<content:encoded><![CDATA[<p>If you are trying to use Code First, you should have seen the database name used by default is not always very useful and you surely want to rename it to use something easier.<br />
To do that, you simply have to modified your Context class ti use the name you&#8217;ve choosen.</p>
<p>Here is a simple MyContext class :</p>
<pre class="brush:csharp">public class MyContext : DbContext
{
	public DbSet&lt;Person&gt; Persons { get; set; }
	public DbSet&lt;Project&gt; Projects { get; set; }
	public DbSet&lt;Task&gt; Tasks { get; set; } 

	public MyContext()
	{}

	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		base.OnModelCreating(modelBuilder);
		modelBuilder.Entity&lt;Task&gt;().HasRequired(t =&gt; t.Project).WithMany(p =&gt; p.Tasks).HasForeignKey(t =&gt; t.ProjectId).WillCascadeOnDelete(false);
		modelBuilder.Entity&lt;Task&gt;().HasRequired(p =&gt; p.AssignedTo);
		modelBuilder.Entity&lt;Person&gt;().Property(d =&gt; d.BirthDate).HasColumnName("Birth");
		modelBuilder.Entity&lt;Project&gt;().Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();
		modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);
	}
}</pre>
<p>This code will generate a database named EFFirstSample.MyContext.</p>
<p>To change the database name I just do this little tweak :<span id="more-1249"></span></p>
<pre class="brush:csharp">public class MyContext : DbContext
{
	public DbSet&lt;Person&gt; Persons { get; set; }
	public DbSet&lt;Project&gt; Projects { get; set; }
	public DbSet&lt;Task&gt; Tasks { get; set; } 

	public MyContext():base("EFSample")
	{}

	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		base.OnModelCreating(modelBuilder);
		modelBuilder.Entity&lt;Task&gt;().HasRequired(t =&gt; t.Project).WithMany(p =&gt; p.Tasks).HasForeignKey(t =&gt; t.ProjectId).WillCascadeOnDelete(false);
		modelBuilder.Entity&lt;Task&gt;().HasRequired(p =&gt; p.AssignedTo);
		modelBuilder.Entity&lt;Person&gt;().Property(d =&gt; d.BirthDate).HasColumnName("Birth");
		modelBuilder.Entity&lt;Project&gt;().Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();
		modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);
	}
}</pre>
<p>if i run my application, a new database will be created, named <strong>EFSample</strong>.<br />
It&#8217;s very simple, there is nothing more to add or to do.</p>
<p>Hope it helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/ef-tips-change-database-name-created-by-code-first/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[EF] Data Annotation and Code Fluent</title>
		<link>http://nadege.deroussen.net/ef-data-annotation-and-code-fluent</link>
		<comments>http://nadege.deroussen.net/ef-data-annotation-and-code-fluent#comments</comments>
		<pubDate>Sun, 15 Apr 2012 20:53:37 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[EF]]></category>
		<category><![CDATA[Code Fluent]]></category>
		<category><![CDATA[Data Annotation]]></category>
		<category><![CDATA[EF CodeFirst]]></category>
		<category><![CDATA[Fluent API]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1184</guid>
		<description><![CDATA[Now that we have learnt how to create a database with Code First and how to declare foreign key relationship, let&#8217;s see what else can be configured. In Code First, there is two ways for configuring database creation. The first one is using Data Annotation as we have seen on my second post about foreign [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have learnt how to <a href="http://drsn.net/n">create a database with Code First</a> and how to <a href="http://drsn.net/6">declare foreign key relationship</a>, let&#8217;s see what else can be configured.</p>
<p>In Code First, there is two ways for configuring database creation. The first one is using Data Annotation as we have seen on my second post about foreign key relationship, the second one is using the Fluent API.</p>
<p>Let&#8217;s see how it works.</p>
<h5>Define MaxLength of a column using DataAnnotation</h5>
<p>First, we will define our columns. If we take a look at the table Project, we can see that all our columns created from string type in our class are nvarchar(max).</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/NvarcharMax.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border-width: 0px;" title="NvarcharMax" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/NvarcharMax_thumb.png" alt="NvarcharMax" width="236" height="95" border="0" /></a></p>
<p><span id="more-1184"></span></p>
<p>It’s not really useful and we don’t want the Name of the project to be so long. So we will define a max lenght for our project name and we will also indicate that a project should have a Name.</p>
<p>Here is how to do it using DataAnnotations.</p>
<pre class="brush:csharp">public class Project
{
    public int ProjectId { get; set; }
    [MaxLength(255)]
    [Required]
    public string Name { get; set; }
    public int ManagerId { get; set; }
    [ForeignKey("ManagerId")]
    public Person Manager { get; set; }
}</pre>
<p>Run the application and check your database columns :</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/Nvarchar2551.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;" title="Nvarchar255" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/Nvarchar255_thumb1.png" alt="Nvarchar255" width="244" height="91" border="0" /></a></p>
<p>This time, the Name column has a max length defined and the column Name is set as Not null.</p>
<h5>Define column’s name using DataAnnotation</h5>
<p>This time, we are going to see how to define a name for a column. Take a look at your Person class :</p>
<pre class="brush:csharp">public class Person
{
	public int PersonId { get; set; }
	public string LastName { get; set; }
	public string FirstName { get; set; }
	public DateTime BirthDate { get; set; }
}</pre>
<p>Let’s say we don’t want the property BirthDate to be stored in a column named BirthDate but in a column named Birth.</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ChangeColumnNameBefore.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border-width: 0px;" title="EF.CF-ChangeColumnNameBefore" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ChangeColumnNameBefore_thumb.png" alt="EF.CF-ChangeColumnNameBefore" width="244" height="110" border="0" /></a></p>
<p>Here is how we can modify your class :</p>
<pre class="brush:csharp">using System.ComponentModel.DataAnnotations;</pre>
<pre class="brush:csharp">public class Person
{
	public int PersonId { get; set; }
	public string LastName { get; set; }
	public string FirstName { get; set; }
	[Column("Birth")]
	public DateTime BirthDate { get; set; }
}</pre>
<p>Run the application and check your database :</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ChangeColumnNameAfter.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border-width: 0px;" title="EF.CF-ChangeColumnNameAfter" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ChangeColumnNameAfter_thumb.png" alt="EF.CF-ChangeColumnNameAfter" width="244" height="107" border="0" /></a></p>
<p>You can see the column name has changed.</p>
<h5>Declare a property as foreign key with DataAnnotation</h5>
<p>First we are going to add a new class Task with a foreign key to Project and to Person classes, add a list of tasks in the Project class and remove the Manager property.</p>
<pre class="brush:csharp">public class Task
{
    public int TaskId { get; set; }
	public string Title { get; set; }
	public string Description { get; set; }
	public int PersonId { get; set; }
	[ForeignKey("PersonId")]
	public Person AssignedTo { get; set; }
	public int ProjectId { get; set; }
	[ForeignKey("ProjectId")]
	public Project Project { get; set; }
}</pre>
<pre class="brush:csharp">public class Project
	{
		public int ProjectId { get; set; }
		MaxLength(255)]
		[Required]
		public string Name { get; set; }
		public List&lt;Task&gt; Tasks { get; set; }
	}</pre>
<p>We use DataAnnotation to specify foreign keys to Project and Person tables. Then we modify your Context class :</p>
<pre class="brush:csharp">public class MyContext : DbContext
    {
        public DbSet&lt;Person&gt; Persons { get; set; }
        public DbSet&lt;Project&gt; Projects { get; set; }
        public DbSet&lt;Task&gt; Tasks { get; set; } 

        public MyContext()
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }</pre>
<p>And we run the console application. Here is the result :</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ProjectTable.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border-width: 0px;" title="EF.CF-ProjectTable" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-ProjectTable_thumb.png" alt="EF.CF-ProjectTable" width="226" height="73" border="0" /></a></p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-TaskTable.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border-width: 0px;" title="EF.CF-TaskTable" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/EF.CF-TaskTable_thumb.png" alt="EF.CF-TaskTable" width="244" height="123" border="0" /></a></p>
<p>Ok, all works fine until now. Let’s re add the Manager property in the Project class :</p>
<pre class="brush:csharp">public class Project
    {
        public int ProjectId { get; set; }
        [MaxLength(255)]
        [Required]
        public string Name { get; set; }

        public int ManagerId { get; set; }
        [ForeignKey("ManagerId")]
        public Person Manager { get; set; }

        public List&lt;Task&gt; Tasks { get; set; }
    }</pre>
<p>Here we get an SqlException:</p>
<p><em>Introducing FOREIGN KEY constraint &#8216;FK_Tasks_Projects_ProjectId&#8217; on table &#8216;Tasks&#8217; may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.</em></p>
<p>Could not create constraint. See previous errors.</p>
<p>This error doesn’t come from Entity Framework nor Code First, it comes from Sql Server. It doesn’t want to create tables and constraints because it estimate there will be some issues if we let it as is. It asks us to specify action in case of delete (to avoid multiple cascade delete) or to modify our contraints.</p>
<p>As we don’t want to modify our contraints, we will specify action in case of delete. Here is the first operation we can’t do using DataAnnotation. We will have to do it using Code Fluent.</p>
<h5>Declare a property as foreign key with Code Fluent</h5>
<p>Now, we have seen how to configure the generation of the database with DataAnnotation, we’ll see how to do the same, and more, with Fluent API.</p>
<p>This configuration should be done in the Context class. Here is how I modify it to declare the foreign key between Task and Project :</p>
<pre class="brush:csharp">public class MyContext : DbContext
    {
        public DbSet&lt;Person&gt; Persons { get; set; }
        public DbSet&lt;Project&gt; Projects { get; set; }
        public DbSet&lt;Task&gt; Tasks { get; set; } 

        public MyContext()
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity&lt;Task&gt;().HasRequired(t =&gt; t.Project).WithMany(p =&gt; p.Tasks).HasForeignKey(t =&gt; t.ProjectId).WillCascadeOnDelete(false);
            modelBuilder.Entity&lt;Task&gt;().HasRequired(p =&gt; p.AssignedTo);
        }
    }</pre>
<p>It’s easy to understand. A task should be linked to a project which could have multiple tasks, this relation would be done using the property ProjectId and there will be no action in case of Delete.</p>
<p>We’ve done the same for the relationship between task and Person.</p>
<p>Now, we need to modify our Task class like this, to remove DataAnnotations :</p>
<pre class="brush:csharp">public class Task
    {
        public int TaskId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        public int PersonId { get; set; }
        public Person AssignedTo { get; set; }

        public int ProjectId { get; set; }
        public Project Project { get; set; }
    }</pre>
<p>And we can remove the directive :</p>
<pre class="brush:csharp">using System.ComponentModel.DataAnnotations;</pre>
<p>This time, when we run the application, there is no error and our tables are correctly created :</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/ProjectFluent.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;" title="ProjectFluent" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/ProjectFluent_thumb.png" alt="ProjectFluent" width="244" height="91" border="0" /></a><a href="http://nadege.deroussen.net/wp-content/uploads/2012/04/TaskFluent.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;" title="TaskFluent" src="http://nadege.deroussen.net/wp-content/uploads/2012/04/TaskFluent_thumb.png" alt="TaskFluent" width="244" height="124" border="0" /></a></p>
<h5>Define column’s name using Code Fluent</h5>
<p>Now, we will modify our Person class to use Code Fluent.</p>
<p>First, we delete the Attribute Column.</p>
<pre class="brush:csharp">public class Person
    {
        public int PersonId { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime BirthDate { get; set; }
    }</pre>
<p>Then we go to the Context class and add the line below :</p>
<pre class="brush:csharp">modelBuilder.Entity&lt;Person&gt;().Property(d =&gt; d.BirthDate).HasColumnName("Birth");</pre>
<h5>Define a column as non nullable</h5>
<p>We will also indicate that a project should have a manager:</p>
<pre class="brush:csharp">modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);</pre>
<h5>Define the MaxLength of a property and that it is a required information</h5>
<pre class="brush:csharp">modelBuilder.Entity&lt;Project&gt;().Property(p =&gt; p.Name).HasMaxLength(255).IsRequired();             modelBuilder.Entity&lt;Project&gt;().HasRequired(p =&gt; p.Manager);</pre>
<p>This article is now finished. I hope it’ll help you understand how DataAnnotation and Code Fluent work.</p>
<p>You can download source here :<br />
<a href='http://nadege.deroussen.net/wp-content/uploads/2012/04/EFThirdSample.zip'>Data Annotation and Code Fluent</a></p>
<p>In a future article we’ll see how to organize our configuration.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/ef-data-annotation-and-code-fluent/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips for Entity Framework Migrations</title>
		<link>http://nadege.deroussen.net/tips-for-entity-framework-migrations</link>
		<comments>http://nadege.deroussen.net/tips-for-entity-framework-migrations#comments</comments>
		<pubDate>Fri, 13 Apr 2012 10:25:30 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[O/R Mapper]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[EF CodeFirst]]></category>
		<category><![CDATA[EF Migration]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1209</guid>
		<description><![CDATA[I just read this post this morning and found it very interesting. Some tips on how to work with EF Migration, what to check, what not to do &#8230; Tips for Entity Framework Migrations Maybe some of you can find it useful too.]]></description>
			<content:encoded><![CDATA[<p>I just read this post this morning and found it very interesting. Some tips on how to work with EF Migration, what to check, what not to do &#8230;</p>
<p><a href="http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/">Tips for Entity Framework Migrations</a></p>
<p>Maybe some of you can find it useful too.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/tips-for-entity-framework-migrations/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Managed Metadata] Error encountered in background cache check System.UnauthorizedAccessException</title>
		<link>http://nadege.deroussen.net/managed-metadata-error-encountered-in-background-cache-check-system-unauthorizedaccessexception</link>
		<comments>http://nadege.deroussen.net/managed-metadata-error-encountered-in-background-cache-check-system-unauthorizedaccessexception#comments</comments>
		<pubDate>Wed, 04 Apr 2012 11:48:18 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[SP2010]]></category>
		<category><![CDATA[Managed Meatadata]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1197</guid>
		<description><![CDATA[When you try to use Managed Metadata Service, if you get the following error on the Term Store : &#8220;The Managed Metadata Service or Connection is currently not available. The Application Pool or Managed Metadata Web Service may not have been started. Please Contact your Administrator.&#8221;, check your ULS log. If you find the following [...]]]></description>
			<content:encoded><![CDATA[<p>When you try to use Managed Metadata Service, if you get the following error on the Term Store : &#8220;The Managed Metadata Service or Connection is currently not available. The Application Pool or Managed Metadata Web Service may not have been started. Please Contact your Administrator.&#8221;, check your ULS log.</p>
<p>If you find the following line with <strong>Taxonomy</strong> category an error code <strong>ca3r</strong></p>
<blockquote><p>Error encountered in background cache check System.UnauthorizedAccessException: The current user has insufficient permissions to perform this operation.</p></blockquote>
<p>The issue should come from permissions. The account use to run the Managed Metadata service should miss some rights.</p>
<p>Here are some explanations on how to configure it with the right permissions :<br />
<a title="Failed Background Check" href="http://james-brennan.co.uk/2011/10/19/failed-background-check-when-the-managed-metadata-service-says-no/" target="_blank">Failed Background Check</a></p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/managed-metadata-error-encountered-in-background-cache-check-system-unauthorizedaccessexception/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SharePoint deployment : Optimization</title>
		<link>http://nadege.deroussen.net/sharepoint-deployment-optimization</link>
		<comments>http://nadege.deroussen.net/sharepoint-deployment-optimization#comments</comments>
		<pubDate>Wed, 04 Apr 2012 11:22:14 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[SP2010]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[force]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1191</guid>
		<description><![CDATA[As every SharePoint developer knows, deployment for SharePoint are sometimes really tricky and you have to be careful on how you manage it. IT Department wants to impact their users as less as possible and to do that, we have to configure our package to limit the down time of the production platform. Here is [...]]]></description>
			<content:encoded><![CDATA[<p>As every SharePoint developer knows, deployment for SharePoint are sometimes really tricky and you have to be careful on how you manage it. IT Department wants to impact their users as less as possible and to do that, we have to configure our package to limit the down time of the production platform.<br />
Here is a really interesting article about how to optimize your deployment regarding the availability of your platform :<br />
<a href="http://blog.mastykarz.nl/optimizing-deploying-sharepoint-packages-minimize-impact-farm-availability/" title="Optimizing the process of deploying SharePoint Packages to minimize the impact on farm’s availability" target="_blank">Optimizing the process of deploying SharePoint Packages to minimize the impact on farm’s availability</a></p>
<p>I also find this article very interesting because it lists some tips really important on my opinion. </p>
<p>The tip really important to me is the use of the <strong>force</strong> attribute :</p>
<blockquote><p>
Never use the Force switch. Not only it makes SharePoint stop affected Application Pools but also it prevents you from seeing errors should there be any. The Force switch might be useful in some scenarios, like fixing broken deployments of SharePoint Packages, but in general you should try to avoid using it.</p></blockquote>
<p>I think this is really important. If you use the <strong>force</strong> attribute you will think that everything went right, but it could be wrong. You just don&#8217;t see any errors. It doesn&#8217;t mean everything went ok. And sometimes, you will encountered some issues, you will spend lot&#8217;s of time to understand why, and will see that some features are not well deployed on all your Web Front End whereas there were no errors during deployments.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/sharepoint-deployment-optimization/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows 8 Consumer Preview installed</title>
		<link>http://nadege.deroussen.net/windows-8-consumer-preview-installed</link>
		<comments>http://nadege.deroussen.net/windows-8-consumer-preview-installed#comments</comments>
		<pubDate>Tue, 06 Mar 2012 08:04:21 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[Actu Techno Microsoft]]></category>
		<category><![CDATA[Win8]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1181</guid>
		<description><![CDATA[I just installed the Consumer Preview of Windows 8 and here are my first impression. Let&#8217;s start with the installation itself. It&#8217;s very very easy. I just put the installation files on a bootable USB key, then I boot on it and the installation began. You have to select a partition enter the windows key [...]]]></description>
			<content:encoded><![CDATA[<p>I just installed the Consumer Preview of Windows 8 and here are my first impression. Let&#8217;s start with the installation itself. It&#8217;s very very easy. I just put the installation files on a bootable USB key, then I boot on it and the installation began. You have to select a partition enter the windows key and that&#8217;s it. I came back later and I see the screen below, asking me to set up settings. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085116.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085116.jpg" alt="20120306-085116.jpg" class="alignnone size-full" /></a></p>
<p><span id="more-1181"></span><br />
I select a color for my background. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085135.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085135.jpg" alt="20120306-085135.jpg" class="alignnone size-full" /></a></p>
<p>I clicked <strong>Next</strong> and went on a screen to connect to a wireless network.<br />
<a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085239.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085239.jpg" alt="20120306-085239.jpg" class="alignnone size-full" /></a><br />
It&#8217;s a good thing to allow user to connect an hidden network from there.<br />
I continued and a new screen purpose me to configure other settings. I choose to use <strong>Express Settings</strong>. I could change them later if I need it.<br />
The next screen helped me to sign in. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085833.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-085833.jpg" alt="20120306-085833.jpg" class="alignnone size-full" /></a><br />
I was a little confused there because I had to sign in with a windows account. I was not able to create an account for my computer. I imagine it&#8217;s because of Azure and cloud computing. We will no more be able to choose what we want to use or not. We will be oblige to be connected. Bad thing !!!<br />
But I continued. My account was created. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090232.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090232.jpg" alt="20120306-090232.jpg" class="alignnone size-full" /></a></p>
<p>My Widows finished to configure all settings. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090312.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090312.jpg" alt="20120306-090312.jpg" class="alignnone size-full" /></a><br />
And I launch Windows 8 on my laptop. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090343.jpg"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/03/20120306-090343.jpg" alt="20120306-090343.jpg" class="alignnone size-full" /></a></p>
<p>Now, I have to play with it and see what&#8217;s new ( lots of things) !</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/windows-8-consumer-preview-installed/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EF Code First : Add foreign key relationship</title>
		<link>http://nadege.deroussen.net/ef-code-first-add-foreign-key-relationship</link>
		<comments>http://nadege.deroussen.net/ef-code-first-add-foreign-key-relationship#comments</comments>
		<pubDate>Mon, 23 Jan 2012 19:31:15 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[EF]]></category>
		<category><![CDATA[EF CodeFirst]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1126</guid>
		<description><![CDATA[In the first article, I show how to create a database with one table using Code First. You can read it here : EF Code First : Simple sample Now, we want to add a new table to work with foreign key. Let&#8217;s add a new Project class like this : public class Project { [...]]]></description>
			<content:encoded><![CDATA[<p>In the first article, I show how to create a database with one table using Code First. You can read it here : <a href="http://nadege.deroussen.net/ef-code-first-simple-sample">EF Code First : Simple sample</a></p>
<p>Now, we want to add a new table to work with foreign key. Let&#8217;s add a new Project class like this :</p>
<pre class="brush:csharp">public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public int ManagerId { get; set; }
    public Person Manager { get; set; }
}</pre>
<p>You can see the Manager property is of type Person and I add ManagerId property to be the key between the two tables.</p>
<p>Now add the DbSet of Project as you&#8217;ve done for Person :</p>
<pre class="brush:csharp">public DbSet Projects { get; set; }</pre>
<p>And run the application.<br />
<span id="more-1126"></span></p>
<p>You should get an InvalidOperationException :<br />
<em>The model backing the &#8216;MyContext&#8217; context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.</em></p>
<p>This is because the changes we made have too more effect on the database and it should be deleted. But the EF Context can&#8217;t decide itself if it can drop the database to recreate it.<br />
What&#8217;s the solution ?<br />
There is two solutions :</p>
<ul>
<li>You can delete the database yourself and rerun the application</li>
<li>You can use the SetInitializer using DropCreateDatabaseIfModelChanges.</li>
</ul>
<p>I won&#8217;t explain the first solution. About the second one, it&#8217;s a feature allowed by Code First. You can ask the context to automatically drop and recreate the database if the model change.<br />
<strong>You should be very careful with this option, because in a production environment, you can loose all your datas. I recommend not to use this in a production environment. It could be very useful for a developer but not to run in a real world.</strong></p>
<p>Because we don&#8217;t want to drop ourself the database, we will see how to tell the context to do it for us.</p>
<p>In the file MyContext.cs, add a new class : MyInitializer</p>
<pre class="brush:csharp">public class MyInitializer : DropCreateDatabaseIfModelChanges
{
}</pre>
<p>As you can see, the database would be dropped and recreated each time the model change.<br />
You can do more things in the initializer but we will see that later in this post.</p>
<p>Now we will set the initializer. Go back to Program.cs and add the following line at the beginning of the Main method :</p>
<pre class="brush:csharp">Database.SetInitializer(new MyInitializer());</pre>
<p>Now, you can run our application.</p>
<p>What has hapenned ? The context has detected changes in the model and so, the database has been deleted and recreated. If you check the tables in the database, you should see 2 tables : <em>People</em> and <em>Project</em>.<br />
The person in the Main method has been created in the People table. The item appears only once because the table was recreated and so, the previous record was deleted and not backed up.</p>
<p>If we look closer, we can see a column named ManagerId and another one named Manager_PersonId.<br />
<div id="attachment_1144" class="wp-caption aligncenter" style="width: 310px"><a href="http://nadege.deroussen.net/wp-content/uploads/2012/01/ProjectTable1.png"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/01/ProjectTable1-300x110.png" alt="" title="Project table with FK auto generated" width="300" height="110" class="size-medium wp-image-1144" /></a><p class="wp-caption-text">Project table with FK auto generated</p></div><br />
The first one was created because of our property <em>ManagerId</em>. The second one was created because of our navigation property to the list of <em>Person</em>. </p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/01/ProjectManager-relationship.png"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/01/ProjectManager-relationship-300x105.png" alt="" title="Project Manager relationship" width="300" height="105" class="aligncenter size-medium wp-image-1143" /></a></p>
<p>EF Code First has created a foreign key for us and it&#8217;s great, but what we&#8217;d want is to use the ManagerId property.<br />
So let&#8217;s modify our Project class to use this property whereas let Code First create it for us.</p>
<p>To configure Code First and the way it will generate our database, there is two methods : <strong>DataAnnotation</strong> and <strong>Fluent API</strong></p>
<p>Let&#8217;s use DataAnnotation.<br />
Modify your class like this :</p>
<pre class="brush:csharp">public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public int ManagerId { get; set; }
    [ForeignKey("ManagerId")]
    public Person Manager { get; set; }
}</pre>
<p>You should add the <strong>System.ComponentModel.DataAnnotations</strong> namespace.<br />
Adding the ForeignKey attribute, we say to Code First that we want the ManagerId properties to be used as Foreign Key to the Person table.</p>
<p>Run the application and check your database schema. The <em>Manager_PersonId</em> column does not exist anymore, there is only the ManagerId column and it is declare as a foreign key to the People table.</p>
<p><a href="http://nadege.deroussen.net/wp-content/uploads/2012/01/Project-columns-with-FK.png"><img src="http://nadege.deroussen.net/wp-content/uploads/2012/01/Project-columns-with-FK.png" alt="" title="Project columns with FK" width="158" height="132" class="aligncenter size-full wp-image-1142" /></a></p>
<p>We just create our first one to many relationship using EF Code First. As we have seen, we can declare the FK in the class or let Code First managed it for us.<br />
In the next article, we will talk about Data Annotation and Code Fluent.</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/ef-code-first-add-foreign-key-relationship/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Series of articles about EF Code First</title>
		<link>http://nadege.deroussen.net/series-of-articles-about-ef-code-first</link>
		<comments>http://nadege.deroussen.net/series-of-articles-about-ef-code-first#comments</comments>
		<pubDate>Thu, 19 Jan 2012 19:13:31 +0000</pubDate>
		<dc:creator>Nadege Deroussen</dc:creator>
				<category><![CDATA[EF]]></category>
		<category><![CDATA[Code Fluent]]></category>
		<category><![CDATA[Data Annotations]]></category>
		<category><![CDATA[EF CodeFirst]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[TPH]]></category>
		<category><![CDATA[TPT]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://nadege.deroussen.net/?p=1121</guid>
		<description><![CDATA[As I begin to work a lot with Entity Framework Code First, I decide to write some articles to share some samples that could be useful for beginners. Here is how I think to organize it : 1 &#8211; First sample : A very simple example on how to create a database with a table [...]]]></description>
			<content:encoded><![CDATA[<p>As I begin to work a lot with Entity Framework Code First, I decide to write some articles to share some samples that could be useful for beginners.<br />
Here is how I think to organize it :</p>
<p>1 &#8211; <a href="http://drsn.net/n">First sample</a> : A very simple example on how to create a database with a table<br />
2 &#8211; <a href="http://drsn.net/6">Add foreign key relationship</a> : We will enhanced the application created on the first tutorial to add relationships.<br />
3 &#8211; <a href="http://drsn.net/7r">Data Annotation and Code Fluent</a><br />
4 &#8211; <a href="http://drsn.net/7v">Code Fluent configuration file</a><br />
4 &#8211; Initialize database with data<br />
5 &#8211; Table per Type and Table per Hierarchy</p>
<p>Tips : <a href="http://drsn.net/7t">How to define your own Database name</a></p>
<p>I hope I&#8217;ll find enough time to write all of this.<br />
Hopefully, I already write the first one !</p>
]]></content:encoded>
			<wfw:commentRss>http://nadege.deroussen.net/series-of-articles-about-ef-code-first/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

