locked
Adding data to Tables with Navigation Properties RRS feed

  • Question

  • User1439689341 posted

    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" />

    <!--Session data-->

    <input onclick="jsCall();" id="jsProxy" type="hidden" />

    Saturday, April 14, 2012 3:44 PM

Answers

  • User3866881 posted

    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。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 15, 2012 9:46 PM
  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2012 9:59 AM

All replies

  • User-1877821760 posted

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=135

    Sunday, April 15, 2012 3:29 PM
  • User3866881 posted

    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。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 15, 2012 9:46 PM
  • User-330204900 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2012 9:59 AM