2

I've tried to solve this fro quite some time already with no luck, guessing it will be a doddle for someone with experience. I'm using the MvcMusicStore.

I have a db context and I have added a new album with a new Genre. If I want to add another album with the same Genre, how do I assign the existing Genre to the new album? As in the second album below. Thanks in advance for any help.

protected override void Seed(Models.MusicStoreDBContext context)
        {
            context.Artists.Add(new Models.Artist { Name = "Al Di Meola" });
            context.Genres.Add(new Models.Genre { Name = "Jazz" });
            context.Albums.Add(new Models.Album
            {
                Artist = new Models.Artist { Name = "Sublime" },
                Genre = new Models.Genre { Name = "Rock" },
                Price = 11.99m,
                Title = "40oz to Freedom"
            });
            context.Albums.Add(new Models.Album
            {
                Artist = new Models.Artist { Name = "Jawbox" },
                Genre = "Rock", // HOW DO I ASSIGN THIS?
                Price = 10.99m,
                Title = "For your own special sweetheart"
            });

3 Answers 3

1

just use a variable ?

var rock = new Models.Genre{Name="Rock"};
 context.Genres.Add(rock);
 context.Albums.Add(new Models.Album
 {
     Artist = new Models.Artist { Name = "Sublime" },
     Genre = rock,
     Price = 11.99m,
     Title = "40oz to Freedom"
 });
 context.Albums.Add(new Models.Album
 {
     Artist = new Models.Artist { Name = "Jawbox" },
     Genre = rock
     Price = 10.99m,
     Title = "For your own special sweetheart"
 });
Sign up to request clarification or add additional context in comments.

Comments

0

Just assign your new Genre to a variable and then set the property to that variable.

var rockGenre = new Models.Genre() { Name = "Rock" };
context.Genres.Add(rockGenre);

context.Albums.Add(new Models.Album
            {
                Artist = new Models.Artist { Name = "Sublime" },
                Genre = rockGenre,
                Price = 11.99m,
                Title = "40oz to Freedom"
            });
            context.Albums.Add(new Models.Album
            {
                Artist = new Models.Artist { Name = "Jawbox" },
                Genre = rockGenre,
                Price = 10.99m,
                Title = "For your own special sweetheart"
            });

1 Comment

Thanks for the answer. I tried it 2 ways as above. This approach worked . I did try the direct access approach first (context.Genres.FirstOrDefault(x=>x.Name=="Rock")) but got an error.
0

Or SubQuery?

protected override void Seed(Models.MusicStoreDBContext context)
    {
        context.Artists.Add(new Models.Artist { Name = "Al Di Meola" });
        context.Genres.Add(new Models.Genre { Name = "Jazz" });
        context.Albums.Add(new Models.Album
        {
            Artist = new Models.Artist { Name = "Sublime" },
            Genre = new Models.Genre { Name = "Rock" },
            Price = 11.99m,
            Title = "40oz to Freedom"
        });
        context.Albums.Add(new Models.Album
        {
            Artist = new Models.Artist { Name = "Jawbox" },
            Genre = (context.Genres.FirstOrDefault(x=>x.Name=="Rock")),
            Price = 10.99m,
            Title = "For your own special sweetheart"
        });

    }

5 Comments

Cheers buddy, this is the first solution I tried as I thought it was the cleanest, however I get the error... "Unable to determine the principal end of the 'MvcMusicStore.Models.Genre_Albums' relationship. Multiple added entities may have the same primary key." What am I missing?
Sounds like a relationship issue with you database. Does album not have a 1:1 relationship with genre?
Many albums to one genre, if that makes sense? Using entity framework and this is MusicStoreDbInitializer, putting initial values in the database.
That makes perfect sense, can you check that relationship isn't many to many by accident, it seems concerned there will be a PK constraint breach
There is a FK for Genre in Albums but not the reverse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.