<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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: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>Comments on: Entity framework 4 &#8211; Code-only, How-to map a combined PK</title>
	<atom:link href="http://daniel.wertheim.se/2009/11/29/entity-framework-4-code-only-how-to-map-a-combined-pk/feed/" rel="self" type="application/rss+xml" />
	<link>http://daniel.wertheim.se/2009/11/29/entity-framework-4-code-only-how-to-map-a-combined-pk/</link>
	<description>Love what you do - Continue to do it - Be eager to learn more about it.</description>
	<lastBuildDate>Tue, 24 Jan 2012 07:50:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Entity framework 4 &#8211; CTP3 &#8211; Code first vs Linq to Sql &#171; Daniel Wertheim</title>
		<link>http://daniel.wertheim.se/2009/11/29/entity-framework-4-code-only-how-to-map-a-combined-pk/#comment-385</link>
		<dc:creator><![CDATA[Entity framework 4 &#8211; CTP3 &#8211; Code first vs Linq to Sql &#171; Daniel Wertheim]]></dc:creator>
		<pubDate>Sun, 16 May 2010 12:40:06 +0000</pubDate>
		<guid isPermaLink="false">http://daniel.wertheim.se/?p=147#comment-385</guid>
		<description><![CDATA[[...] Entity framework 4 – Code-only, How-to map a combined PK [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Entity framework 4 – Code-only, How-to map a combined PK [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Wertheim</title>
		<link>http://daniel.wertheim.se/2009/11/29/entity-framework-4-code-only-how-to-map-a-combined-pk/#comment-20</link>
		<dc:creator><![CDATA[Daniel Wertheim]]></dc:creator>
		<pubDate>Sun, 29 Nov 2009 19:55:37 +0000</pubDate>
		<guid isPermaLink="false">http://daniel.wertheim.se/?p=147#comment-20</guid>
		<description><![CDATA[I sent you an email. I hope it will solve the issue.

For others. You have to include properties in car that holds the value of the Id in Country and LicensePlate.

[sourcecode language=&quot;csharp&quot;]
    [Serializable]
    public class Car
    {
        private LicencePlate _licencePlate;
        private Country _country;

        public LicencePlate LicencePlate
        {
            get { return _licencePlate; }
            set
            {
                _licencePlate = value;
                LicencePlateId = value != null ? value.Id : 0;
            }
        }


        public Country Country
        {
            get { return _country; }
            set
            {
                _country = value;
                CountryId = value != null ? value.Id : 0;
            }
        }

        public int LicencePlateId { get; private set; }

        public int CountryId { get; private set; }

        public string Color { get; set; }

        public Car()
        {
        }
    }

    [Serializable]
    public class LicencePlate
    {
        public int Id { get; set; }
        public string Plate { get; set; }
    }

    [Serializable]
    public class Country
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
[/sourcecode]

[sourcecode language=&quot;csharp&quot;]
    [Serializable]
    public class LicencePlateMapping : EntityConfiguration&lt;LicencePlate&gt;
    {
        public LicencePlateMapping()
        {
            HasKey(l =&gt; l.Id);
            Property(l =&gt; l.Id).IsIdentity();
            Property(l =&gt; l.Plate).IsRequired();
        }
    }

    [Serializable]
    public class CountryMapping : EntityConfiguration&lt;Country&gt;
    {
        public CountryMapping()
        {
            HasKey(c =&gt; c.Id);
            Property(c =&gt; c.Id).IsIdentity();
            Property(c =&gt; c.Name).IsRequired();
        }
    }

    [Serializable]
    public class CarMapping : EntityConfiguration&lt;Car&gt;
    {
        public CarMapping()
        {
            HasKey(c =&gt; new { c.CountryId, c.LicencePlateId });

            Property(c =&gt; c.Color);

            Relationship&lt;LicencePlate&gt;(c =&gt; c.LicencePlate).IsRequired().HasConstraint((c, l) =&gt; c.LicencePlateId == l.Id);
            Relationship&lt;Country&gt;(c =&gt; c.Country).IsRequired().HasConstraint((car, country) =&gt; car.CountryId == country.Id);
        }
    }
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I sent you an email. I hope it will solve the issue.</p>
<p>For others. You have to include properties in car that holds the value of the Id in Country and LicensePlate.</p>
<pre class="brush: csharp;">
    [Serializable]
    public class Car
    {
        private LicencePlate _licencePlate;
        private Country _country;

        public LicencePlate LicencePlate
        {
            get { return _licencePlate; }
            set
            {
                _licencePlate = value;
                LicencePlateId = value != null ? value.Id : 0;
            }
        }

        public Country Country
        {
            get { return _country; }
            set
            {
                _country = value;
                CountryId = value != null ? value.Id : 0;
            }
        }

        public int LicencePlateId { get; private set; }

        public int CountryId { get; private set; }

        public string Color { get; set; }

        public Car()
        {
        }
    }

    [Serializable]
    public class LicencePlate
    {
        public int Id { get; set; }
        public string Plate { get; set; }
    }

    [Serializable]
    public class Country
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
</pre>
<pre class="brush: csharp;">
    [Serializable]
    public class LicencePlateMapping : EntityConfiguration&lt;LicencePlate&gt;
    {
        public LicencePlateMapping()
        {
            HasKey(l =&gt; l.Id);
            Property(l =&gt; l.Id).IsIdentity();
            Property(l =&gt; l.Plate).IsRequired();
        }
    }

    [Serializable]
    public class CountryMapping : EntityConfiguration&lt;Country&gt;
    {
        public CountryMapping()
        {
            HasKey(c =&gt; c.Id);
            Property(c =&gt; c.Id).IsIdentity();
            Property(c =&gt; c.Name).IsRequired();
        }
    }

    [Serializable]
    public class CarMapping : EntityConfiguration&lt;Car&gt;
    {
        public CarMapping()
        {
            HasKey(c =&gt; new { c.CountryId, c.LicencePlateId });

            Property(c =&gt; c.Color);

            Relationship&lt;LicencePlate&gt;(c =&gt; c.LicencePlate).IsRequired().HasConstraint((c, l) =&gt; c.LicencePlateId == l.Id);
            Relationship&lt;Country&gt;(c =&gt; c.Country).IsRequired().HasConstraint((car, country) =&gt; car.CountryId == country.Id);
        }
    }
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arne</title>
		<link>http://daniel.wertheim.se/2009/11/29/entity-framework-4-code-only-how-to-map-a-combined-pk/#comment-19</link>
		<dc:creator><![CDATA[Arne]]></dc:creator>
		<pubDate>Sun, 29 Nov 2009 17:00:46 +0000</pubDate>
		<guid isPermaLink="false">http://daniel.wertheim.se/?p=147#comment-19</guid>
		<description><![CDATA[Hi,

OK this helps :).
But what if, to build on your example:

public class LicensePlate{
  public int ID {get;set;}
  public string Plate{get;set;}
}

public class Country{
  public int ID{get;set;}
  public string Name{get;set;}
}

public class Car{
  public LicensePlate LicensePlate{get;set;}
  public Country Country{get;set;}
  public int SomeProperty{get;set;}
}
So you&#039;ll have a join table with extra fields.

To map the Car I would expect: HasKey(c =&gt; new { LicensePlate_ID = c.LicensePlate.ID, Country_ID = c.Country.ID });

However, the key expression is not valid.

Greets,
Arne]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>OK this helps <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
But what if, to build on your example:</p>
<p>public class LicensePlate{<br />
  public int ID {get;set;}<br />
  public string Plate{get;set;}<br />
}</p>
<p>public class Country{<br />
  public int ID{get;set;}<br />
  public string Name{get;set;}<br />
}</p>
<p>public class Car{<br />
  public LicensePlate LicensePlate{get;set;}<br />
  public Country Country{get;set;}<br />
  public int SomeProperty{get;set;}<br />
}<br />
So you&#8217;ll have a join table with extra fields.</p>
<p>To map the Car I would expect: HasKey(c =&gt; new { LicensePlate_ID = c.LicensePlate.ID, Country_ID = c.Country.ID });</p>
<p>However, the key expression is not valid.</p>
<p>Greets,<br />
Arne</p>
]]></content:encoded>
	</item>
</channel>
</rss>

