1

I am just starting to learn Entity Framework code-first. I am trying to use the .Include syntax to get related objects. I have been able to successfully retrieve objects into a list from a one to many relationship, but can't get it to work from a many to one relationship. So I have a song, which has a related orchestra and a related singer

The object definitions

public class Song
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("OrchestraId")]
    public Orchestra Orchestra { get; set; }
    public int OrchestraId { get; set; }
    [ForeignKey("SingerId")]
    public Singer Singer { get; set; }
    public int SingerId { get; set; }
    public int Genre { get; set; }
    public string Year { get; set; }

    public Song()
    {
        this.Orchestra = new Orchestra();
        this.Singer = new Singer();
    }
}

public class Singer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Orchestra
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I have a context object

public class POCContext : DbContext
{
    public DbSet<Singer> Singers { get; set; }
    public DbSet<Orchestra> Orchestras { get; set; }
    public DbSet<Song> Songs { get; set; }
}

The code I use to get a song is the following

    public Song GetSong(int songId)
    {
        Song song = new Song();

        song = _context.Songs.Include(s => s.Orchestra)
            .Include(s => s.Singer)
            .Single(s => s.Id == songId);
        return song;
    }

When I put a breakpoint on the return statement and look at the song, the song object is populated, but not the singer or orchestra objects.

I had a look at the following related question here but I have not been able to work out what I am doing wrong. Any help is greatly appreciated.

regards Carl

3
  • Does the song actually have related data in the db, is there Fk's, does it actually have data Commented Aug 7, 2018 at 3:48
  • Shortly, don't initialize non collection navigation properties like Song.Orchestra and Song.Singer with new … - there is a big chance that you mess up the EF processing. Remove the Song constructor and try again. Let me know to reopen the question if that's not the case. Commented Aug 7, 2018 at 9:01
  • 1
    Ivan Stoev, Thanks that fixed my issue. I didnt know enough to understand it was the initialization that was causing the problem! Commented Aug 7, 2018 at 9:43

1 Answer 1

0

Orchestra and Singer should be virtual.

public virtual Orchestra Orchestra { get; set; }

public virtual Singer Singer { get; set; }

Here's a sample configuration:

public class Song
{
    [Key]
    public Guid Id { get; set; }

    public string Title { get; set; }

    //navigation property
    public virtual List<Singer> Singers { get; set; }
}


public class Singer
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("Song")]
    [Required]
    public string SongId { get; set; }

    [Required]
    public string Name { get; set; }

    //navigation property
    public virtual Song Song { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

There is no requirement navigation properties to be virtual.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.