Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:38:42+00:00 2026-05-25T23:38:42+00:00

(ASP.net MVC 3.0 Razor, EF Code First) My first question is: where do I

  • 0

(ASP.net MVC 3.0 Razor, EF Code First)

My first question is: where do I store my data? in the Initializer or in the .sdf?

And my second question is this:
I have a class for Songs, and one for Artists.

This is from my Initializer:

var Artists= new List<Artist>()
        {
            new Artist{ Name = "ArtistName", Birthday=DateTime.Parse("4-4-1988")}
        };
        Authors.ForEach(s => context.Authors.Add(s));
        context.SaveChanges();

        var Songs= new List<Song>()
        {
            new Song { Name="SongName", Genre = Genre.GenreTypes.Rock, Artist=Artists[0]} //<--
        };
        Books.ForEach(s => context.Books.Add(s));
        context.SaveChanges();

Where the green arrow is, is the problem.
I’m trying to get the artist by the list-index above (where I loaded the artists into the database), but when I test it (item.Artist.Name), I don’t get anything. the artist property is null! I don’t get it. why? do I even do it correctly? (I’m trying to get the artist’s name, that’s all)

And last question:
I also have in the author’s class a List (list of his songs). how should I load songs of him there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:38:43+00:00Added an answer on May 25, 2026 at 11:38 pm

    What do you mean? Store your data? Your data is normally saved in a database that you chose in your connectionString. The initializer will take care of the database creation, seeding etc.

    Selecting a song with his artist

    You can get the Artist by including him in the query. For example:

     yourContext.Songs.Include("Artist").toList()' 
    

    This will return all the songs and the artist will be filled in for each song.

    Selecting a song with his artist

    In the same way you fill in the Artist for the song.

    yourContext.Artists.Include("ListPropertyName").toList()
    

    Here is a link that might help you:

    http://msdn.microsoft.com/en-us/library/bb896272.aspx

    Saving a song together with his artist.

    I’m going to show the way I work , Your relationship should be something like this:

    public class Artist
        {
            [Key]
            public Guid ID { get; set; }
    
            //..OtherProperties ...
    
            public virtual ICollection<Songs> Songs { get; set; }
        }
    
    public class Song
        {
            [Key]
            public Guid ID { get; set; }
    
            //..OtherProperties ...
    
    
            //Foreign Key Stuff
            [ForeignKey("Artist")]
            public Guid ArtistId { get; set; }
    
            //Relationship
            public virtual Artist Artist { get; set; }
        }
    

    If you work this way you can just fill in the ArtistId manually in the song.
    So You will have to do the following:

    var Artists= new List<Artist>()
            {
                new Artist{ Name = "ArtistName", Birthday=DateTime.Parse("4-4-1988"), ID = Guid.NewGuid()}
            };
            Authors.ForEach(s => context.Authors.Add(s));
    
            var Songs= new List<Song>()
            {
                new Song { Name="SongName", Genre = Genre.GenreTypes.Rock, ArtistId=Artists[0].ID} //<--
            };
            Songs.ForEach(s => context.Songs.Add(s));
    
            context.SaveChanges();
    

    Now, what is the advance with separating the ID and the actual Artist. It’s very simple, when you make a Artist you can manually set the ID and you can use it later in your code instead of saving the artist first. So first you make your artists and set there Id and then you can directly use it in your song. In your example you actually do Savechanges() twice. If you’re using ‘my’ method you only have to save once. You should really check some tutorials on Entity Framework , google is your friend 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework 4.1 code first with ASP.NET MVC 3 and Razor
I was reading about asp.net mvc 3 and saw this piece razor view code
I am using ASP.NET MVC Razor And Data Annotation validators My model: public class
I have the following asp.net mvc 3 razor code, where item is my model,
ASP.NET MVC - what is this in VB.NET? Html.TextBox( name, null, new { @class
ASP.NET MVC 2 will support validation based on DataAnnotation attributes like this: public class
i am working in asp.net mvc 3 web application which uses razor code. and
I have a ASP.Net MVC app with Razor. I try to access a collection
I am using s#arp architecture 2.0 with asp.net mvc 3.0. The razor code in
I am using Telerik Date Picker for ASP.Net MVC Razor Form. In this form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.