Setting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2>Hi,</font></p> <p align=left>I want to edit an entity (eg Business) via a databound form. Amongst its other properties this entity has properties which are themselves entities (eg BusinessType). I want to place a combo on the form for editing the BusinessType property. I'd set its datasource to a list of BusinessType, and it's DisplayMember to the Name property of the BusinessType. I'm not sure what to do with its ValueMember so that it will return the actual entity corresponding to the name selected in the combo box, and then update the databound BusinessType property of the Business entity accordingly. </p> <p align=left> </p> <p align=left>Kev</p>© 2009 Microsoft Corporation. All rights reserved.Sun, 22 Nov 2009 10:10:17 Zd66ae159-75f6-4a38-b111-c7d5e6980599http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d66ae159-75f6-4a38-b111-c7d5e6980599http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d66ae159-75f6-4a38-b111-c7d5e6980599Kevin Donovanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Kevin%20DonovanSetting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2>Hi,</font></p> <p align=left>I want to edit an entity (eg Business) via a databound form. Amongst its other properties this entity has properties which are themselves entities (eg BusinessType). I want to place a combo on the form for editing the BusinessType property. I'd set its datasource to a list of BusinessType, and it's DisplayMember to the Name property of the BusinessType. I'm not sure what to do with its ValueMember so that it will return the actual entity corresponding to the name selected in the combo box, and then update the databound BusinessType property of the Business entity accordingly. </p> <p align=left> </p> <p align=left>Kev</p>Sun, 31 Aug 2008 00:24:03 Z2008-09-10T06:01:02Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#c7433780-40f2-4390-ba7b-d6a17d231df3http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#c7433780-40f2-4390-ba7b-d6a17d231df3MatthieuMEZILhttp://social.msdn.microsoft.com/Profile/en-US/?user=MatthieuMEZILSetting a property which is an entity via a databound combobox.<p>I do a small test without problem:</p><font color="#2b91af" size=2> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left><span style="color:blue"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div><font color="#2b91af" size=2> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left><span style="color:blue">public</span> <span style="color:blue">partial</span> <span style="color:blue">class</span> <span style="color:#2b91af">Form1</span> : <span style="color:#2b91af">Form</span></p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>{</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>    <span style="color:#2b91af">Product</span> product;</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>    <span style="color:blue">public</span> Form1()</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>    {</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        InitializeComponent();</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        <span style="color:blue">var</span> context = <span style="color:blue">new</span> <span style="color:#2b91af">NorthwindEntities</span>();</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        comboBox1.DataSource = context.Categories;</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        comboBox1.DisplayMember = <span style="color:#a31515">&quot;CategoryName&quot;</span>;</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        product = context.Products.First();</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>        comboBox1.DataBindings.Add(<span style="color:#a31515">&quot;SelectedItem&quot;</span>, product, <span style="color:#a31515">&quot;Category&quot;</span>);</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>    }</p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left>}</p></font> <p align=left> </p></div></div> <p align=left> </p></span> <p></p> <p style="font-size:10pt;background:white;margin:0px;color:black;font-family:Courier New" align=left></p></font><font face=Arial size=2></font>Sun, 31 Aug 2008 17:57:02 Z2008-08-31T17:57:02Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#fcabf9d3-3706-412d-bd31-0c04869a58a3http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#fcabf9d3-3706-412d-bd31-0c04869a58a3Kevin Donovanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Kevin%20DonovanSetting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2>Hmm, </font></p> <p align=left>I'm using the infragistics combo control, which doesn't have a SelectedItem property, guess that could be my problem. I'll post a question in the infragistics forum.</p> <p align=left> </p> <p align=left>Thanks,</p> <p align=left>Kevin</p>Sun, 31 Aug 2008 21:49:22 Z2008-08-31T21:49:22Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#62e2a10d-0a9d-4676-adb9-98dc1421519fhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#62e2a10d-0a9d-4676-adb9-98dc1421519fpcchttp://social.msdn.microsoft.com/Profile/en-US/?user=pccSetting a property which is an entity via a databound combobox.<font size=2><span style="font-family:Arial">Imagine this situation:<br>2 tables<br>first Products<br>Id int<br>Name nvarchar(50)<br>CategoryId int<br><br>second: Category<br>Id int<br>Name nvarchar(50)<br><br>this 2 tables are related: Products.CategoryId-&gt;Category.Id<br><br>I generated entities using Ado .net entity framework technology.<br>For table Products field CategoryId is transformed into navigation properties wich return an object Category<br><br>I want to edit products using DataGridView and specially ,i want to set CategoryId with a ComboBox <br>(with tradition way i receive an exception and i don't know what to do)<br><br>Your opinion may help me so please share it with me.<br><br><br>Best Regards<br>Claudiu<br><br></span></font><font size=2><span style="font-family:Arial"><br><br></span></font>Mon, 01 Sep 2008 18:03:34 Z2008-09-01T18:03:34Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#f68a9417-472d-4eea-930d-eaefd212ed8ehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#f68a9417-472d-4eea-930d-eaefd212ed8eArilson Santoshttp://social.msdn.microsoft.com/Profile/en-US/?user=Arilson%20SantosSetting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2></font> </p>I have the same problem with DataGridView - DataBound - ComboBoxWed, 03 Sep 2008 15:28:03 Z2008-09-03T15:28:03Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#39b445b0-bd8b-4cff-af7c-146c8ea70657http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#39b445b0-bd8b-4cff-af7c-146c8ea70657pcchttp://social.msdn.microsoft.com/Profile/en-US/?user=pccSetting a property which is an entity via a databound combobox.<font size=2><span style="font-family:Arial">Good!<br>We are two.<br>Now let's find some help.<br>For at least 1 week i am looking for an answer for my problem and nothing.<br>I hope it's somebody who want to share with us his </span></font>opinion.<br><font size=2><span style="font-family:Arial"> <br>Claudiu<br></span></font>Wed, 03 Sep 2008 16:51:47 Z2008-09-03T16:51:47Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#5bf37846-a905-4cbf-abef-242fadff9d2dhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#5bf37846-a905-4cbf-abef-242fadff9d2dEric Dettinger - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Eric%20Dettinger%20-%20MSFTSetting a property which is an entity via a databound combobox.<p align=left>Hi Claudiu,</p> <p align=left> </p> <p align=left>The way I'm interpreting your earlier post, you have a combo box and a data grid view. You want to fill the combo box with the Id of each Category entity. Whenever the selection changes in the combo box, you want to fill the data grid view with all the products whose id matches the selected Id.</p> <p align=left> </p> <p align=left>If I've interpreted this correctly, the following code will do what you want. It's written for Northwind, which looks very similar to the schema in your situation.</p> <p align=left> </p><font size=2> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>NorthwindEntities</font></font><font size=2> _context = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font><font size=2> </font><font color="#2b91af" size=2><font color="#2b91af" size=2>NorthwindEntities</font></font><font size=2>();</font></p> <p align=left><font size=2> </p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font><font size=2> </font><font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> DetailsViewForm_Load(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> sender, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>EventArgs</font></font><font size=2> e)</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>ObjectResult</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Categories</font></font><font size=2>&gt; categories = </font></p> <blockquote dir=ltr style="margin-right:0px"> <p align=left><font size=2>_context.Categories.Execute(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MergeOption</font></font><font size=2>.NoTracking);</font></p></blockquote> <p align=left><font size=2>categoriesComboBox.DataSource = categories;</font></p> <p align=left><font size=2>categoriesComboBox.DisplayMember = </font><font color="#a31515" size=2><font color="#a31515" size=2>&quot;CategoryID&quot;</font></font><font size=2>;</p></blockquote> <p>}</p> <p align=left> </p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>private</font></font><font size=2> </font><font color="#0000ff" size=2><font color="#0000ff" size=2>void</font></font><font size=2> categoriesComboBox_SelectedIndexChanged(</font><font color="#0000ff" size=2><font color="#0000ff" size=2>object</font></font><font size=2> sender, </font><font color="#2b91af" size=2><font color="#2b91af" size=2>EventArgs</font></font><font size=2> e)</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>Categories</font></font><font size=2> category = categoriesComboBox.SelectedItem </font><font color="#0000ff" size=2><font color="#0000ff" size=2>as</font></font><font size=2> </font><font color="#2b91af" size=2><font color="#2b91af" size=2>Categories</font></font><font size=2>;</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>if</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>null</font></font><font size=2> == category)</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>return</font></font><font size=2>;</p></blockquote> <p></font><font color="#2b91af" size=2><font color="#2b91af" size=2>ObjectQuery</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Products</font></font><font size=2>&gt; products = (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>ObjectQuery</font></font><font size=2>&lt;</font><font color="#2b91af" size=2><font color="#2b91af" size=2>Products</font></font><font size=2>&gt;)_context.Products</p> <blockquote dir=ltr style="margin-right:0px"> <p>.Where(c =&gt; c.Categories.CategoryID == category.CategoryID);</p></blockquote> <p>productsGridView.DataSource = products.Execute(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MergeOption</font></font><font size=2>.NoTracking);</p></blockquote> <p>}</p></font>Fri, 05 Sep 2008 23:01:50 Z2008-09-05T23:01:50Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#dbcc3182-3a81-4fa0-9f62-761954832bb5http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#dbcc3182-3a81-4fa0-9f62-761954832bb5pcchttp://social.msdn.microsoft.com/Profile/en-US/?user=pccSetting a property which is an entity via a databound combobox.<span id="_ctl0_MainContent_PostFlatView"><span>I know!I don't speak english very well and maybe this it's the reason why you  don't understand my problem.<br><br>In DataGridView i want to edit Products. in one field in this DataGridView is CategoryId wich edited  </span></span><span id="_ctl0_MainContent_PostFlatView"><span>with the help of  DataGridViewComboBoxCell</span></span><span id="_ctl0_MainContent_PostFlatView"><span> filled with Categories<br><br>The generated entitie Products has the folowing public members:<br>Id Int32<br>Name string<br>CategorytId typeof(Categories)(it's navigation properties and return an object))<br><br></span></span> <div style="text-align:left"><span id="_ctl0_MainContent_PostFlatView"><span></span></span>I set the members of <span id="_ctl0_MainContent_PostFlatView"><span>DataGridViewComboBoxCell</span></span><span id="_ctl0_MainContent_PostFlatView"><span> :<br>DataSource=Categories<br></span></span><span id="_ctl0_MainContent_PostFlatView"><span></span></span></div><span id="_ctl0_MainContent_PostFlatView"><span>DisplayMember=&quot;Name&quot;<br>ValueMember=&quot;Id&quot;? <br>Selecting a value from this </span></span><span id="_ctl0_MainContent_PostFlatView"><span>DataGridViewComboBoxCell</span></span><span id="_ctl0_MainContent_PostFlatView"><span> must set the CategoryId field from DataGridView (it's navigation properties and returns an entitie)</span></span><br><span id="_ctl0_MainContent_PostFlatView"><span><br>and i get this error<br><br><br><br><br></span></span><span id="_ctl0_MainContent_PostFlatView"><span>SystemArgumentException DataGridViewComboBoxCell value is not valid<br><br><br><br>Claudiu<br><br><br></span></span>Sat, 06 Sep 2008 06:17:16 Z2008-09-06T06:17:16Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d44dede1-9498-4655-970a-344a7afef30ehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d44dede1-9498-4655-970a-344a7afef30eDiego B Vegahttp://social.msdn.microsoft.com/Profile/en-US/?user=Diego%20B%20VegaSetting a property which is an entity via a databound combobox.<p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>Claudiu,</font></p> <p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>Databinding works well to display and update objects that have properties of primitive types (i.e. that can be easily rendered to a simple control), but as you have noticied, Entity Framework represents relationships between objects as object references, instead of the typical key values. </font></p> <p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>Fortunately, Entity Framework code-generated entity classes also contain a feature that allows reading and setting the keys of a related entity without actually using the object reference: EntityReferences. </font></p> <p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>In your Product entity you will see a property named CategoryReference. And CategoryReference has a property named EntityKey. You cannot use databinding directly with EntityReferences, but you can extend the entity classes in a partial class in order to add those additional properties for databinding. </font></p> <p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>This is a very simple example that uses the Product entity of a standard Northwind model:</font></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System.Collections.Generic;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System.Linq;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System.Text;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System.Data;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">using</span><span style="font-size:10pt;font-family:Consolas"> System.ComponentModel;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><font face=Calibri size=3></font> </p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:Consolas">namespace</span><span style="font-size:10pt;font-family:Consolas"> NorthwindModel</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas">{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas"><span>    </span><span style="color:blue">partial</span> <span style="color:blue">class</span> <span style="color:#2b91af">Products</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas"><span>    </span>{</span></p><span style="font-size:10pt;font-family:Consolas"><span> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>        </span><span style="color:green">//this property would be redundant in serialization</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>        </span>[<span style="color:blue">property</span>: <span style="color:#2b91af">NonSerialized</span>]</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>        </span><span style="color:blue">public</span> <span style="color:blue">int</span>? CategoryReference_CategoryID</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>        </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span><span style="color:blue">get</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span><span style="color:blue">if</span> (<span style="color:blue">this</span>.CategoryReference == <span style="color:blue">null</span>)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:blue">return</span> <span style="color:blue">null</span>;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span><span style="color:blue">if</span> (<span style="color:blue">this</span>.CategoryReference.EntityKey == <span style="color:blue">null</span>)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:blue">return</span> <span style="color:blue">null</span>;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span><span style="color:blue">return</span> (<span style="color:blue">int</span>?)<span style="color:blue">this</span>.CategoryReference.EntityKey.EntityKeyValues[0].Value;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span><span style="color:blue">set</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span><span style="color:blue">if</span> (<span style="color:blue">value</span>.HasValue)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:green">//To notify others listening that the property is changing</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:blue">this</span>.OnPropertyChanging(<span style="color:#a31515">&quot;CategoryReference_CategoryID&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:green">//Always needs to create a new EntityKey object</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:blue">this</span>.CategoryReference.EntityKey = </span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                        </span><span style="color:blue">new</span> <span style="color:#2b91af">EntityKey</span>(</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span> </span><span>                           </span><span style="color:#a31515">&quot;NorthwindEntities.Categories&quot;</span>, </span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                            </span><span style="color:blue">new</span> <span style="color:#2b91af">EntityKeyMember</span>[] </span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                                </span>{ <span style="color:blue">new</span> <span style="color:#2b91af">EntityKeyMember</span>(<span style="color:#a31515">&quot;CategoryID&quot;</span>, </span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas">                                    (<span style="color:blue">object</span>)<span style="color:blue">value</span>.Value) });</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:green">//To notify others listenging that the property has changed</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                    </span><span style="color:blue">this</span>.OnPropertyChanged(<span style="color:#a31515">&quot;CategoryReference_CategoryID&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>                </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>            </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal"><span style="font-size:10pt;font-family:Consolas"><span>        </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left>    </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas">}</span></p></div></div> <p align=left> </p></span> <p></p> <p class=MsoNormal style="margin:0in 0in 0pt;line-height:normal" align=left><span style="font-size:10pt;font-family:Consolas"></span></p> <p class=MsoNormal style="margin:0in 0in 10pt"><font face=Calibri size=3>Hope this answers your question,<br>Diego</font></p> <p align=left><font face=Arial size=2></font> </p>Sun, 07 Sep 2008 01:34:44 Z2008-09-10T06:01:02Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#3a39d2ff-3452-4876-a454-6359bea0130fhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#3a39d2ff-3452-4876-a454-6359bea0130fpcchttp://social.msdn.microsoft.com/Profile/en-US/?user=pccSetting a property which is an entity via a databound combobox.<span id="_ctl0_MainContent_PostFlatView"><span><font size=2><span style="font-family:Arial">Ado .Net Entity Data Model it's a good technology.The generated classes are good things for programmers.<br>Anyway , the code what you show me could be very well generated(if i have 50 tables with 3 navigation properties each ,i must write this code again and again ...)</span></font><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"><span style="color:green"><span style="width:100%"> <span style="color:rgb(51,51,51)">or controls used to to edit this entities (like </span></span></span></span></span><span style="color:rgb(51,51,51)"></span><b style="color:rgb(51,51,51)"><a id="_ctl0_MainContent__ctl0_PostForm_ReplySubject" href="../msdn/ShowPost.aspx?PostID=3842359&amp;SiteID=1">DataGridViewComboColumn</a></b><span style="color:rgb(51,51,51)"><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"><span style="width:100%">) could be upgraded to </span></span></span>recognize NavigationProperties and have the properly behaviour.Maybe in next release of sp.<br><br>Thanks for your kindness.<br><br></span></span><font size=2><span style="font-family:Arial">Claudiu<br></span></font>Sun, 07 Sep 2008 15:22:13 Z2008-09-07T15:22:13Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#0c3f1e58-1dc1-4b67-858c-4f78c2008547http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#0c3f1e58-1dc1-4b67-858c-4f78c2008547Arilson Santoshttp://social.msdn.microsoft.com/Profile/en-US/?user=Arilson%20SantosSetting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2></font> </p> <p>But Diego,</p> <p align=left> </p> <p align=left>I tried something like that and I still have a problem.</p> <p align=left>In a combo the &quot;Description&quot; doesn't appear but just its ID.</p>Thu, 11 Sep 2008 13:09:45 Z2008-09-11T13:09:45Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#65c05add-18c9-495b-9a7f-348722badcf1http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#65c05add-18c9-495b-9a7f-348722badcf1pcchttp://social.msdn.microsoft.com/Profile/en-US/?user=pccSetting a property which is an entity via a databound combobox.<font size=2><span style="font-family:Arial">try setting DisplayMember of ComboBox<br><br>for me  the supplied snippet code worked just fine.<br><br>claudiu<br></span></font>Thu, 11 Sep 2008 13:13:55 Z2008-09-11T13:13:55Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#527d7e4e-1ccc-4d4e-8ce4-da04f8cbfbb9http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#527d7e4e-1ccc-4d4e-8ce4-da04f8cbfbb9Arilson Santoshttp://social.msdn.microsoft.com/Profile/en-US/?user=Arilson%20SantosSetting a property which is an entity via a databound combobox.<p>Diego,</p> <p align=left> </p> <p align=left>Yours solutcion worked here at least the DataGridViewCombo show the &quot;description&quot;</p> <p align=left>but if I change the DataGridViewCombo's value the change doesn't occur.</p> <p align=left>Ex.</p> <p align=left>Description   Value</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>A          1</p> <p align=left>B          2</p></blockquote> <p dir=ltr align=left> </p> <p dir=ltr align=left>If I change from A to B (in a DataGridViewComboBoxColumn) and get the value, it still is 1.</p> <p align=left><font face=Arial size=2></font> </p>Thu, 11 Sep 2008 14:29:20 Z2008-09-11T14:29:20Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#48708045-a925-4972-a09f-50e6344c01dehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#48708045-a925-4972-a09f-50e6344c01deAnneMMAhttp://social.msdn.microsoft.com/Profile/en-US/?user=AnneMMASetting a property which is an entity via a databound combobox.<p>Diego,</p> <p align=left> </p> <p align=left>Your code example for working with entity references was very helpful. I have an additional question about binding, though. You indicated that it works well to display and update object properties that have primitive types. Although I can get my bound controls (text boxes in this case) to display the data in an object, the data isn't updated automatically when I make changes to the values in the controls. Instead, I have to manually assign the values to the object properties. Am I missing something or is this the way this is supposed to work?</p> <p align=left> </p> <p align=left>Anne </p> <p align=left><font face=Arial size=2></font> </p>Thu, 11 Sep 2008 18:44:09 Z2008-09-11T18:44:09Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#7c0c7b45-17c6-483c-8dc6-3bdfaae7f3c6http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#7c0c7b45-17c6-483c-8dc6-3bdfaae7f3c6Arilson Santoshttp://social.msdn.microsoft.com/Profile/en-US/?user=Arilson%20SantosSetting a property which is an entity via a databound combobox.<p align=left><font face=Arial size=2>Sincerally, I'll return to Linq To Sql <img height=19 alt=Sad src="http://forums.microsoft.com/MSDN/emoticons/emotion-6.gif" width=19></font></p>Fri, 12 Sep 2008 12:32:06 Z2008-09-12T12:32:06Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d94d9c63-b080-4c02-8c84-02c921bc8a28http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#d94d9c63-b080-4c02-8c84-02c921bc8a28Yann Duranhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yann%20DuranSetting a property which is an entity via a databound combobox. Hi Diego,<br><br>Are you *seriously* suggesting that the only way to bind to a combobox using EF is to *manually* extend every generated class that you need to use in binding?<br><br>I sincerely hope not!<br><br>I'm having trouble at the moment, binding navigation properties to comboboxes &amp; am looking for the right way to do it (or a way that works), but I hope I don't have to do what you've suggested, that's *crazy*.<br><br>YannThu, 05 Feb 2009 01:34:10 Z2009-02-05T01:34:10Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#72f968ee-613f-4589-ba71-571b6e7744d6http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#72f968ee-613f-4589-ba71-571b6e7744d6Yann Duranhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yann%20DuranSetting a property which is an entity via a databound combobox. Diego,<br><br>Well, at least I managed to bind an EF entity's navigattion property to a WinForms combobox now, using the method you described (had to bind the SelectedValue property of the combobox to the newly created property) &amp; I'm able to display &amp; edit values correctly.<br><br>Could you tell me *why* these &quot;properties&quot; are not generated automatically as suggested by Claudiu?<br><br>Will this be changed at some point? An update to EF 1? Or in EF 2?<br><br>Seems to be quite an oversight, seeing as this is such a basic requirement &amp; as so much else is already generated. I would suggest, that anything that needs to be &quot;extended&quot; over &amp; over, as in this case, is a good candidate for inclusion in the automatically generated code. <br><br>Even if there was an &quot;option&quot; to generate it or not, to appease anyone who is already concerned about the current amount of generated code.<br><br>Thanks,<br><br>YannFri, 06 Feb 2009 00:57:28 Z2009-02-06T00:57:28Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#b9b872c1-86b9-4090-b78f-604ce7a64874http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#b9b872c1-86b9-4090-b78f-604ce7a64874Diego B Vegahttp://social.msdn.microsoft.com/Profile/en-US/?user=Diego%20B%20VegaSetting a property which is an entity via a databound combobox. Yann,<br><br>It is actually a great question. In .NET 3.5 SP1, the EDM (the conceptual model on which EF operates) models relationships as independent objects from entities. The fact that there is a FK that has a value that matches a PK in the database is kind of an implementation detail that we abstract away in the model. I understand this doesn't work very well with the way many are used to think about database applications, including databinding scenarios in which the FKs are handy, but on the ORM camp, this is a relatively common pattern.<br><br>Supporting FKs alongside navigation properties as valid representation of relationships poises a few challenges. You may be interested in the following EF Design blog post in which we ask for feedback on this subject d <a href="http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx">http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx</a>. <br><br>Thanks,<br>Diego<hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Wed, 18 Feb 2009 00:03:59 Z2009-02-18T00:03:59Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ba57a661-b613-4d4a-837a-4ce34e8dbeddhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ba57a661-b613-4d4a-837a-4ce34e8dbeddJosh56http://social.msdn.microsoft.com/Profile/en-US/?user=Josh56Setting a property which is an entity via a databound combobox.Diego - I read through this thread as well as the EF blog responses in the link you just posted.  This is all very interesting, and I can see both sides of the argument for FK's, however, at this point, how do I bind an entity as the datasource for a combobox in a datagrid when I can't set the ValueMember property in the databinding?  The issue is as you pointed out there is no FK exposed on the related table, which is fine- I'm ok with using the hydrated entity which is already available since I loaded the FK table's data into the drop down via EF- however, there is not a navigation property available for the related table.  I think at the very least if a navigation property with the name of the entity was exposed (i.e. &quot;tblProductTypes&quot;) and could be set as the valuemember in a combobox databinding scenario, my needs would be met, and the ORM principles would also be preserved since a real entity would exist, and a real object relationship would exist.  At this point I'm hard pressed to recommend to my team that we go forward with EF with such a glaring shortfall- manually creating a partial class of the EntityObject to handle this FK scenario is just not an acceptable solution.<br>Thanks Wed, 18 Feb 2009 01:15:41 Z2009-02-18T01:15:41Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#6d26d2b1-cfc2-4eff-b87a-75b9bd6833aehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#6d26d2b1-cfc2-4eff-b87a-75b9bd6833aeYann Duranhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yann%20DuranSetting a property which is an entity via a databound combobox. Hi Diego,<br><br>Thanks for taking the time to answer my question (well, sort of, lol). I understand that things sometimes need to change when a new paradigm gets introduced. What I don't understand is how MS can expect developers to use that new paradigm if they can't *easily* do the things that they need to do &amp; are used to doing.<br><br>Binding to a combobox is such a basic operation, that seems to have been overlooked. It's all well &amp; good to &quot;abstract away the FK details&quot;, but leaving developers hanging, with no replacement way (&amp; a *manual* way does *not* count) of doing what is required in their apps is not only &quot;not acceptable&quot; (as Josh rightly points out), it's also highly likely to severely curb the number of people willing to move to EF (again as Josh has pointed out for his own team).<br><br>I hope my &quot;great question&quot; will give the EF developers encouragement to give consideration as to how developers are supposed to use EF entities with controls such as comboboxes.<br><br>If any of the people using EF in serious apps have found a non-manual way, it would be good to publish their method for all developers to consider. Right now it's kind of &quot;everyone roll your own method&quot;. That's crazy!<br><br>YannWed, 18 Feb 2009 01:29:00 Z2009-02-18T01:29:00Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a57b6b3b-3541-4410-9d43-86644e74ff5chttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a57b6b3b-3541-4410-9d43-86644e74ff5cDiego B Vegahttp://social.msdn.microsoft.com/Profile/en-US/?user=Diego%20B%20VegaSetting a property which is an entity via a databound combobox.<p>Yann and Josh,</p> <p>I can assure you that our team agrees this an important issue. Great thanks for your feedback, and please stay tuned. </p> <p>Diego<br><br>PS: On the particular issue that Josh describes, I am not sure I understand the explanation. By default there is going to be a navigation property that will hold a reference to the object on the other end. For instance, Product has a Category navigation property, and Post has an Author navigation property. The fact is that generally speaking, databinding doesn't know how to handle properties that are not of primitive types. </p> <p><br><br> </p><hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Wed, 18 Feb 2009 09:54:07 Z2009-02-18T09:54:07Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#b7122361-bc15-40a8-b6a1-23817bdb964ahttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#b7122361-bc15-40a8-b6a1-23817bdb964aJosh56http://social.msdn.microsoft.com/Profile/en-US/?user=Josh56Setting a property which is an entity via a databound combobox.<p> Diego- thanks for responding, I basically was describing the same issue as Yann- i.e.:</p> <p>If you had these two tables:</p> <p>tblProduct (pkProductID, Description, Price, fkTypeID)</p> <p>tblType (pkTypeID, TypeDescription)</p> <p>The Entity Sets created for these tables would look like:</p> <p>tblProduct (scalar[pkProductID, Description, Price] Navigation [tblType])</p> <p>tblType (scalar[pkTypeID, TypeDescription] Navigation [tblProduct])</p> <p>So, in my datagrid that is bound to tblProduct, and has a column that is of type combobox which is bound to tblType, what value do I set for the ValueMember?  Like you said, generally speaking you can only bind the valuemember property to a primitive type, yet you could not in this case choose pkTypeID, since the tblProduct entity expects an object reference.  I know there is not a good answer for this problem right now, I just wanted to clarify my question.</p> <p>Thanks,</p> <p>Josh</p> <p>   </p>Wed, 18 Feb 2009 15:50:21 Z2009-02-18T15:50:21Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#5ee14da7-dec2-4ad7-85ae-846b3ed89ed2http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#5ee14da7-dec2-4ad7-85ae-846b3ed89ed2AlexS-Dhttp://social.msdn.microsoft.com/Profile/en-US/?user=AlexS-DSetting a property which is an entity via a databound combobox.<p>Diego,<br/><br/>I'm also having this problem! (It's really not easy!)<br/>I've followed your steps above, but I'm at a loss and simply do not understand. The above code is placed into its own class, within the same project as the edmx model... Hopefully, I've got this right. But I can't figure out<br/><br/><span style="color:blue">[<span style="color:blue">property</span>: <span style="color:#2b91af">NonSerialized</span>]<br/>public</span> <span style="color:blue">int</span>? CategoryReference_CategoryID</p> <p>I get an error &quot;Attribute 'NonSerialized' is not valid on this declaration type. It is only valid on 'field' declarations.&quot; for my own project<br/><br/>Within my project I have tblCountry and have found the reference to tblTimeZoneReference but no property for EntityKey</p> <pre lang="x-c#"> [global::System.ComponentModel.BrowsableAttribute(false)] [global::System.Runtime.Serialization.DataMemberAttribute()] public global::System.Data.Objects.DataClasses.EntityReference&lt;tblTimeZones&gt; tblTimeZonesReference { get { return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference&lt;tblTimeZones&gt;(&quot;IBISModel.FK_tblCountry_tblTimeZones&quot;, &quot;tblTimeZones&quot;); } set { if ((value != null)) { ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference&lt;tblTimeZones&gt;(&quot;IBISModel.FK_tblCountry_tblTimeZones&quot;, &quot;tblTimeZones&quot;, value); } } } </pre> <p>Please could somebody point out what I'm doing wrong!!!!</p> <p>All I want to do is update my table tblCountry with a foreign key (Combobox) to tblTimeZone in WPF using EF in a WCF Service.<br/>I can get the list of TimeZones, and display the correct relationship within Country but when I update I get an error:<br/>&quot;The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.&quot;<br/><br/>The update that fails:<br/><br/>        public void Update(tblCountry dsCountry)<br/>        {<br/><br/>            context.Attach(dsCountry);<br/>            dsCountry.SetAllModified(context); // custom extension method<br/><br/>            try<br/>            {<br/><br/>                context.SaveChanges(true);<br/>            }<br/>            catch (OptimisticConcurrencyException e)<br/>            {<br/>                // someone else changed the data, throw<br/>                throw (e);<br/>            }<br/>        }</p> <p>So now I'm here in this thread, after 3 weeks!! of trying to get it sorted. Please Please help! :S</p> <p> </p> <p> </p>Thu, 07 May 2009 09:46:28 Z2009-05-07T09:52:27Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#3cd2865c-a896-41c4-93a8-bf944de967a9http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#3cd2865c-a896-41c4-93a8-bf944de967a9MagicMaxhttp://social.msdn.microsoft.com/Profile/en-US/?user=MagicMaxSetting a property which is an entity via a databound combobox.<p>Hello!</p> <p>AlexS-D - did you found the solution for your problem? I have the same issue related with ComboBox bounded and Update on server side....</p> <p>Thank you!</p>Mon, 18 May 2009 10:02:37 Z2009-05-18T10:02:37Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ddb3ab53-0a88-4cd3-8e06-90d8c953aa46http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ddb3ab53-0a88-4cd3-8e06-90d8c953aa46Anil Mujagichttp://social.msdn.microsoft.com/Profile/en-US/?user=Anil%20MujagicSetting a property which is an entity via a databound combobox.Any news on this issue?<br/> <br/> Thanks...Tue, 16 Jun 2009 23:37:11 Z2009-06-16T23:37:11Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a02dadd5-e452-49cb-a492-8a81bd5fed32http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a02dadd5-e452-49cb-a492-8a81bd5fed32gaunmanuelhttp://social.msdn.microsoft.com/Profile/en-US/?user=gaunmanuelSetting a property which is an entity via a databound combobox.<p>Diego.<br/>This problem is from August 2008...... today is july 2009 One year has passed, and no solution.<br/><br/>How can I use a DataGridViewComboBoxColumn within a dataGridView?<br/>Do you have an example? of course using EF.<br/><br/>Thanks..</p>Thu, 02 Jul 2009 17:00:32 Z2009-07-02T17:00:32Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#610321a6-7924-4f04-bcaf-8f385c57f5efhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#610321a6-7924-4f04-bcaf-8f385c57f5efAnil Mujagichttp://social.msdn.microsoft.com/Profile/en-US/?user=Anil%20MujagicSetting a property which is an entity via a databound combobox.<blockquote> <p>Diego.<br/> This problem is from August 2008...... today is july 2009 One year has passed, and no solution.<br/> <br/> How can I use a DataGridViewComboBoxColumn within a dataGridView?<br/> Do you have an example? of course using EF.<br/> <br/> Thanks..</p> </blockquote> <br/> Hi Diego,<br/> <br/> Maybe you can add &quot;Self&quot; read-only property to your entity which will return the object itself:<br/> <br/> public Customer Self<br/> {<br/>     get<br/>     {<br/>         return this;<br/>     }<br/> }<br/> <br/> I found this in recent book I read. It's not great solution because you have to add it to every entity where you need it, but at least it is workaround until we get VS2010 and more control over generated code from EDM Designer.<br/> <br/> Hope it helps!<br/> <br/> Regards!<br/> <br/> Anil<br/>Fri, 03 Jul 2009 12:20:45 Z2009-07-03T12:20:45Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a723b7b5-75b9-40b1-a277-618201032515http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#a723b7b5-75b9-40b1-a277-618201032515Anil Mujagichttp://social.msdn.microsoft.com/Profile/en-US/?user=Anil%20MujagicSetting a property which is an entity via a databound combobox.<blockquote>Hi Diego,<br/> <br/> Maybe you can add &quot;Self&quot; read-only property to your entity which will return the object itself:<br/> <br/> public Customer Self<br/> {<br/>     get<br/>     {<br/>         return this;<br/>     }<br/> }<br/> <br/> I found this in recent book I read. It's not great solution because you have to add it to every entity where you need it, but at least it is workaround until we get VS2010 and more control over generated code from EDM Designer.<br/> <br/> Hope it helps!<br/> <br/> Regards!<br/> <br/> Anil<br/></blockquote> ... :) I wanted to say: Hi gaunmanuel...<span class=fullbadge><a class=author rel=nofollow href="../../../../../Profile/en-US/?user=gaunmanuel&amp;referrer=http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599/?prof%3drequired&amp;rh=I84XZnjyNs6Ot19vsCGV3d6sIydE4Rn4EpvvS4Rdo6c%3d&amp;sp=forums" class=author></a> </span>Fri, 03 Jul 2009 12:23:50 Z2009-07-03T12:23:50Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#bfa5ea72-154f-4e70-95b3-2e2a2c5f66f1http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#bfa5ea72-154f-4e70-95b3-2e2a2c5f66f1Diego B Vegahttp://social.msdn.microsoft.com/Profile/en-US/?user=Diego%20B%20VegaSetting a property which is an entity via a databound combobox.<p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">Hello gaunmanuel &amp; others,<br/><br/>I have already responded to you in a new thread. People have had apparently mixed results in trying to follow the workaround proposed, but I can confirm that it works, so I would recommend to take a look at the code in the post marked and answer and try to adapt it to your case. </span><span style="font-family:'Times New Roman','serif';color:black;font-size:12pt"></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">A few things that I know might go wrong:</span><span style="font-family:'Times New Roman','serif';color:black;font-size:12pt"></span></p> <p class=MsoListParagraph style="text-indent:-0.25in;margin:auto 0in auto 42pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt"><span style="">1.<span style="font:7pt &quot;Times New Roman&quot;">     </span></span></span><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">The sample code applies to Products – Categories as included in the sample Northwind database. In order to make this work in your model you will need to use the right names for properties and also in strings.</span></p> <p class=MsoListParagraph style="text-indent:-0.25in;margin:auto 0in auto 42pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt"><span style="">2.<span style="font:7pt &quot;Times New Roman&quot;">     </span></span></span><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">The code corresponds to a partial class file for &quot;Products&quot; (the default name obtained after creating the model from the Northwind database), that needs to be compiled alongside the Products type generated by EF.</span></p> <p class=MsoListParagraph style="text-indent:-0.25in;margin:auto 0in auto 42pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt"><span style="">3.<span style="font:7pt &quot;Times New Roman&quot;">     </span></span></span><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">Due to a limitation in WinForms design-time databinding, properties added in partial classes are not picked up at design time unless you compile it in a separate assembly. The solution in this case would be to move the EDMX model file, the generated code and also the proposed code for the partial class to a separate assembly, for instance, to a class library, and then add a reference to it from the project containing the form. </span></p> <p class=MsoListParagraph style="text-indent:-0.25in;margin:auto 0in auto 42pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt"><span style="">4.<span style="font:7pt &quot;Times New Roman&quot;">     </span></span></span><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">If the key exposed is also part of the primary key of the entity, it will not be possible to change the value. For instance, in the Northwind sample database, ProductID is a foreign key in [Order Details], but it is also part of the primary key, and therefore will be read-only in the entity.</span></p> <p class=MsoListParagraph style="margin:auto 0in"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">Besides this, we have added support for Foreign Key association in .NET 4.0. This is not included in beta 1, but will be available in the next public drops. Foreign Key associations make it possible to have both a navigation property and scalar FK properties associated with the same relationship. This should make this kind of databinding workaround unnecessary.</span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">Hope this helps,</span><span style="font-family:'Times New Roman','serif';color:black;font-size:12pt"></span></p> <p class=MsoNormal style="line-height:normal;margin:0in 0in 10pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt">Diego </span><span style="font-family:'Times New Roman','serif';color:black;font-size:12pt"></span></p> <div class=MsoNormal style="text-align:center;line-height:normal;margin:0in 0in 6pt"><span style="font-family:'Verdana','sans-serif';color:black;font-size:8pt"> <hr size=2> </span></div> <p class=MsoNormal style="margin:0in 0in 10pt"><span style="line-height:115%;font-family:'Verdana','sans-serif';color:black;font-size:8pt">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.</span></p>Mon, 06 Jul 2009 08:03:59 Z2009-07-06T09:04:03Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#1e863957-0382-4c34-9591-c397f953a8behttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#1e863957-0382-4c34-9591-c397f953a8beSe3ker385http://social.msdn.microsoft.com/Profile/en-US/?user=Se3ker385Setting a property which is an entity via a databound combobox.I think that people are looking at this the wrong way, this problem is not the limitation of Entity framework, but rather a limitation of WinForms Combobox control.<br/><br/>The combobox returns only primitive types, thats why it cant return an entity object.<br/><br/>Combox has two modes, unbound and databound, in databound mode you set its DataSource, DisplayMember and ValueMember properties. So if you set the ValueMember becuse return type is an object the combobox cannot process it.<br/><br/>3rd party developers, like for instance DevExpress, they seperated unbound mode and databound mode to two controls, one is ordinary Combobox other is a LookupEditor, The lookup editor also has 3 properties DataSource, DisplayMember and ValueMember, if you set the DataSourceto EF entity list and set its DisplayMember, but leave the ValueMember blank the LookupEditor returns an object, the relatanship.<br/><br/>In the next version id rather that relationships remain 1st class and that the Combobox changes its functionality, then the EF changes to include FK's so we can bind to Combobox. <br/><br/><hr class="sig">VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/Mon, 06 Jul 2009 11:13:18 Z2009-07-06T11:13:18Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ae66c8e8-4a62-4d4c-8d83-2c2a564056d8http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#ae66c8e8-4a62-4d4c-8d83-2c2a564056d8Yann Duranhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yann%20DuranSetting a property which is an entity via a databound combobox.Hi <span class=name><span style="color:#008000;font-size:small">Se3ker385</span></span><span class=affil> </span>,<br/><br/>You're quite right, I hadn't considered it from that angle. On the other hand though, the &quot;workaround&quot; does show that it's possible to make the existing combo box work. My only complaint was that it had to be done manually.<br/><br/>Thanks for reminding us of the other side of the equation.<br/><br/>YannMon, 06 Jul 2009 11:53:51 Z2009-07-06T11:53:51Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#0ce7b6a9-eb4f-4346-835a-b6a495437df5http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#0ce7b6a9-eb4f-4346-835a-b6a495437df5Gentledepphttp://social.msdn.microsoft.com/Profile/en-US/?user=GentledeppSetting a property which is an entity via a databound combobox.<p>Hi!</p> <p>I had the same problem and found the solution:</p> <p>In our architecture, we convert our Entities to Dtos (POCOs) and vice versa. For a clean architecture the EntityKey property (which is clearly Entity Framework internal) is not serialized.</p> <p>Therefore, when we convert all Dtos to Entities again, we need to set the EntityKey properties of the References to match those entities that already exist in the database. Otherwhise they would be recreated.</p> <pre lang="x-c#">Customer.AddressReference.EntityKey = orig.EntityKey;</pre> <p>However a problem arises, when the Customer entity that came in, already has an Address entity set that has no EntityKey either, which almost always is the case, as we send the object graph to client and back again. If you set the EntityKey on the &quot;AddressReference&quot; property in this case, it is NOT SET on the entity in the &quot;Address&quot; property. Therefore you will geht the error:<br/>&quot;the object could not be added or attached because its EntityReference has an EntityKey blablabla...&quot;<br/>when you want to save the Customer entity.</p> <p>Therefore you need to do the following workaround:</p> <pre lang="x-c#">//temporary save incoming value var address = Customer.Address //Remove the Address entity from Customer Customer.Address = null; //set the entity key Customer.AddressReference.EntityKey = new EntityKey(&quot;MyDemo.Addresses&quot;, &quot;AddressID&quot;, address.AddressID);</pre> <p>Hope that helps you guys. ;)</p> <p><strong>Warning</strong>: If you set the AddressReference.EntityKey first and remove the Address afterwards, the EntityKey will be null again!</p> <p><strong>Warning</strong>: If you set the EntityKey on the Address Entity directly, you will get the error, that there already exists another entity in the ObjectContext with the same key.</p> <p>As you see - I tried 'em all ;)</p>Mon, 19 Oct 2009 07:16:16 Z2009-10-19T07:16:16Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#1b69855d-0087-4830-84a2-90b1ce9b2c45http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d66ae159-75f6-4a38-b111-c7d5e6980599#1b69855d-0087-4830-84a2-90b1ce9b2c45Eric Commelinhttp://social.msdn.microsoft.com/Profile/en-US/?user=Eric%20CommelinSetting a property which is an entity via a databound combobox.Hello,<br/><br/>First, I am quite a beginner with Entity Framework, so excuse me if I say something realy silly.<br/>I had the same DataGriwViewComboBoxColumn problem while testing Entity Framework.<br/>The proposed solution seemed quite complicated, I used something else.<br/>Because I hate the DataGridView control (being mostly an ASP.Net developper, I find it very difficult to use when I need it, one a year...), I assumed it was a grid problem and not an entity framework problem.<br/>The DataGridViewComboBoxColumn filled with categories does not use the entity object as ValueMember, so the navigation property of the product cannot be databound.<br/>I used an &quot;adapter&quot;. Here is a small test winform : <pre lang="x-c#">public partial class FormTest : Form { private NorthwindEntities Northwind; public FormTest() { InitializeComponent(); this.Northwind = new NorthwindEntities(); DataGridViewComboBoxColumn cbcCategories = new DataGridViewComboBoxColumn(); cbcCategories.DataPropertyName = &quot;Categories&quot;; cbcCategories.DisplayMember = &quot;CategoryName&quot;; cbcCategories.ValueMember = &quot;TheCategoryItself&quot;; cbcCategories.DataSource = Northwind.Categories.Select( c =&gt; new { CategoryName = c.CategoryName, TheCategoryItself = c } ); DgvProducts.Columns.Add( cbcCategories ); DataGridViewTextBoxColumn tbcProductName = new DataGridViewTextBoxColumn(); tbcProductName.DataPropertyName = &quot;ProductName&quot;; DgvProducts.Columns.Add( tbcProductName ); DgvProducts.AutoGenerateColumns = false; DgvProducts.DataSource = Northwind.Products; } protected override void OnClosed( EventArgs e ) { base.OnClosed( e ); Northwind.SaveChanges(); } } </pre> <br/>Everything seems OK but the code is to simple and I am a newbie so it is (very) possible I missed something.<br/><br/>Best regards (and sorry for my french english).<br/><br/>Eric.<br/><br/>Sun, 22 Nov 2009 10:10:17 Z2009-11-22T10:12:18Z