mercredi 22 juin 2016

Mvc Music Store (MVC3) : SampleData won't populate DB

Here's my really BIG problem: When I run the program it finds NO genres ): Check the picture

The example is using this code as the connection string:

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

As you might notice, It is using SqlServerCe4.0, but instead I change it in order to use SqlClient (wich I have already installed):

<connectionStrings>
     <add 
            name="MusicStoreEntities"
             connectionString="Data Source=(localdb)\v11.0;
             Integrated Security=true;AttachDbFileName=|DataDirectory|MvcMusicStore.mdf"
             providerName="System.Data.SqlClient" 
      />
</connectionStrings>

Sample Data file:

using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>
    {
        protected override void Seed(MusicStoreEntities context)
        {
            var genres = new List<Genre>
            {
                //Perfec genres
            };

            var artists = new List<Artist>
            {
                //Cool Artists list
            };

            new List<Album>
            {
                //Awesome albums
            }.ForEach(a => context.Albums.Add(a));

        }
    }
}

A context class:

using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
    }
}

Application Start class:

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcMusicStore
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
    }
}

Any ideas of how to solve this problem? I'd appriciate your help.

Aucun commentaire:

Enregistrer un commentaire