locked
MVC Music Store database connection issue RRS feed

  • Question

  • User-429092344 posted

    Hello, I really need some help for Music Store tutorial...I am following the asp.net MvcMusicStore tutorial and everything works fine but the data is not adding to MvcMusicStore.sdf...This is the page with that part of the tutorial that makes the connection to database: http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4 The expected result should return a list with all of genres, but in my case I'm getting NULL values in MvcMusicStore.sdf Genres table.

    I downloaded the completed tutorial and everything works fine there, all data values are inserted to database tables..

    SampleData.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    namespace MvcMusicStore.Models
    {
        public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>
        {
            protected override void Seed(MusicStoreEntities context)
            {
                var genres = new List<Genre>
                {
                    new Genre { Name = "Rock" },
                    new Genre { Name = "Jazz" },
                    new Genre { Name = "Metal" }
                };
    
                var artists = new List<Artist>
                {
                    new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
                    new Artist { Name = "Aaron Goldberg" }
                };
    
                new List<Album>
                {
                    new Album { Title = "A Copland Celebration, Vol. I", Genre = genres.Single(g => g.Name == "Classical"), Price = 8.99M, Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra"), AlbumArtUrl = "/Content/Images/placeholder.gif" },
                }.ForEach(a => context.Albums.Add(a));
            }
        }
    }

    Web.config:

      <connectionStrings>
        <add name="MusicStoreEntities"
         connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
         providerName="System.Data.SqlServerCe.4.0"/>
      </connectionStrings>

    Global.asax

    protected void Application_Start()
    {
        System.Data.Entity.Database.SetInitializer(
            new MvcMusicStore.Models.SampleData());
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    Genre.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcMusicStore.Models
    {
        public partial class Genre
        {
            public int GenreId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public List<Album> Albums { get; set; }
        }
    }
    Monday, August 31, 2015 7:59 AM

All replies

  • User-986267747 posted

    Hi pavelv

    pavelv

    public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>

    According to your description, I create a sample and also get this problem. This is because you are using "DropCreateDatabaseIfModelChanges" . If your DB is already created and you just want to add initial data then using "DropCreateDatabaseAlways" . However, you must realise this will recreate the DB from scratch every time an application restarts. So only use this when your DB is already created and you don't want to change the model (and you don't care about losing the data already in the DB) AND then change it to one of the other two options. If your DB is not created , you could use the "CreateDatabaseIfNotExists" , it's ok.

    I hope it’s helpful to you.

    Best Regards

    Klein zhang

    Tuesday, September 1, 2015 4:11 AM