Adding data to Tables with Navigation Propertieshttp://forums.asp.net/t/1792930.aspx/1?Adding+data+to+Tables+with+Navigation+PropertiesMon, 16 Apr 2012 13:59:07 -040017929304932687http://forums.asp.net/p/1792930/4932687.aspx/1?Adding+data+to+Tables+with+Navigation+PropertiesAdding data to Tables with Navigation Properties <p>I have a created Entity Framework.I am trying to add a new songs and New album to the table. For example when I try to add a new song I used the method below and it tells me &quot;Object reference not set to an instance of an object&quot; on the s.Artist.ArtistName. I have a webform that shows a list of Artists in a drop down menu and now I want to add a new song for that artist. How do I do that?</p> <p></p> <p></p> <pre class="prettyprint">public Song AddNewSongNav(String ArtistName, String SongName) { using (var context = new MyEntities()) { var s = new Song(); s.SongTitle = SongName; s.Artist.ArtistName = ArtistName; s.Artist.WikipediaUrl=&quot;http://en.wikipedia.org/Testing&quot;; context.Songs.AddObject(s); context.SaveChanges(); return s; } }</pre> <p>The frame work for the Artist Table has ArtistID,ArtistName and WikiPediaURL. It has a navivgation property for Album and Song. It is linked to Song as 1 to many and Album as 1 to Many.</p> <p>It has a Song table, which has a SongID, SongTitle and Artist_ArtistID. It has a navigation property of Artist and Album. It's linked as a many to many to the Albums table.</p> <p>Finally the Album table has an AlbumID,AlbumTitle,CoverArt,Year,Genre,MimeType and Artist_ArtistID. It has a navigation property of Artist and Song.</p> <p><br> <br> </p> <p>&lt;input id=&quot;gwProxy&quot; type=&quot;hidden&quot; /&gt;</p> <p>&lt;input onclick=&quot;jsCall();&quot; id=&quot;jsProxy&quot; type=&quot;hidden&quot; /&gt;</p> &lt;div id=&quot;refHTML&quot;&gt;&lt;/div&gt; 2012-04-14T19:44:07-04:004933447http://forums.asp.net/p/1792930/4933447.aspx/1?Re+Adding+data+to+Tables+with+Navigation+PropertiesRe: Adding data to Tables with Navigation Properties <p>http://www.dotnetcurry.com/ShowArticle.aspx?ID=135</p> 2012-04-15T19:29:40-04:004933569http://forums.asp.net/p/1792930/4933569.aspx/1?Re+Adding+data+to+Tables+with+Navigation+PropertiesRe: Adding data to Tables with Navigation Properties <p>Hello:)</p> <p>Solutions</p> <p>Check it before doing inserting an object into s.ArtistI suspect whether Artist is null or not</p> <pre class="prettyprint">using (var context = new MyEntities()) { var s = new Song(); s.SongTitle = SongName; if(s.Artist==null) { s.Artist = new Artist(); } s.Artist.ArtistName = ArtistName; s.Artist.WikipediaUrl=&quot;http://en.wikipedia.org/Testing&quot;; context.Songs.AddObject(s); context.SaveChanges(); return s; }</pre> <p>Reasons</p> <p>s.Artist may be null</p> 2012-04-16T01:46:42-04:004934883http://forums.asp.net/p/1792930/4934883.aspx/1?Re+Adding+data+to+Tables+with+Navigation+PropertiesRe: Adding data to Tables with Navigation Properties <p>if I understand you correctly you have an Edit or Insert page in DD (Dynmaic Data) and this has several drop down lists for selecting entries for the page but you need a methos where you can add a new entry to one or more of the drop dwon lists?</p> <p>if this is correct you can use my <a href="http://csharpbits.notaclue.net/2009/12/popup-insert-control-for-dynamic-data.html"> A Popup Insert control for Dynamic Data</a>&nbsp;which allows this in DD.</p> 2012-04-16T13:59:07-04:00