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 "Object reference not set to an instance of an object" 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?
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="http://en.wikipedia.org/Testing";
context.Songs.AddObject(s);
context.SaveChanges();
return s;
}
}
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.
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.
Finally the Album table has an AlbumID,AlbumTitle,CoverArt,Year,Genre,MimeType and Artist_ArtistID. It has a navigation property of Artist and Song.
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?
Michael20
0 Points
1 Post
Adding data to Tables with Navigation Properties
Apr 14, 2012 07:44 PM|LINK
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 "Object reference not set to an instance of an object" 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?
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="http://en.wikipedia.org/Testing"; context.Songs.AddObject(s); context.SaveChanges(); return s; } }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.
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.
Finally the Album table has an AlbumID,AlbumTitle,CoverArt,Year,Genre,MimeType and Artist_ArtistID. It has a navigation property of Artist and Song.
<input id="gwProxy" type="hidden" />
<input onclick="jsCall();" id="jsProxy" type="hidden" />
<div id="refHTML"></div>hiza808
Member
270 Points
75 Posts
Re: Adding data to Tables with Navigation Properties
Apr 15, 2012 07:29 PM|LINK
http://www.dotnetcurry.com/ShowArticle.aspx?ID=135
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Adding data to Tables with Navigation Properties
Apr 16, 2012 01:46 AM|LINK
Hello:)
【Solutions】
Check it before doing inserting an object into s.Artist——I suspect whether Artist is null or not…
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="http://en.wikipedia.org/Testing"; context.Songs.AddObject(s); context.SaveChanges(); return s; }【Reasons】
s.Artist may be null。
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Adding data to Tables with Navigation Properties
Apr 16, 2012 01:59 PM|LINK
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?
if this is correct you can use my A Popup Insert control for Dynamic Data which allows this in DD.
Always seeking an elegant solution.