[RESOLVED]MVC 3 – Entity framework – create database

Hi,

I finished this MVC 3 tutorial
http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1
.

Than I deleted database file in App_Data folder.

In Global.asax.cs -> Application_Start I have

System.Data.Entity.Database.SetInitializer(new MyStore.Models.SampleData());

this is supposed to "prepare" a data for database and at the first query the database is created.

But when I run/debug, I get an error "An exception occurred while initializing the database. See the InnerException for details." in HomeController in method GetTopSellingAlbums().

HomeController code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyStore.Models;

namespace MyStore.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        StoreEntities storeDB = new StoreEntities();
        public ActionResult Index()
        {

            //System.Data.Entity.Database.SetInitializer(new MyStore.Models.SampleData());
            
            // Get most popular albums
            var albums = GetTopSellingAlbums(14);

            return View(albums);
        }
        private List<Album> GetTopSellingAlbums(int count)
        {
            // Group the order details by album and return
            // the albums with the highest count
            return storeDB.Albums
                .Take(count)
                .ToList();
        }
    }
}

SampleData.cs

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

namespace MyStore.Models
{
    public class SampleData : CreateDatabaseIfNotExists<StoreEntities>
    {
        protected override void Seed(StoreEntities context)
        {
.
.
.
}
}
}

Why the database isn’t created?

Perinho

Why the database isn’t created?

Music store uses code first approach. Hence it should create the database. I suspect you may be missing something, like connection string etc. I suggest download the pdf document from the below link and follow the chapter 4

https://mvcmusicstore.codeplex.com/

All I did was delete database file in Solution Explorer. Connection strings are untouched.

Web.config :

<connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)v11.0;Initial Catalog=aspnet-MyStore-20141022120832;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnet-MyStore-20141022120832.mdf" />
    <add name="MyStoreEntities" connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf" providerName="System.Data.SqlServerCe.4.0"/> 
  </connectionStrings>

How can I force the database to be created before GetTopSellingAlbums() is called in HomeController?

And why is HomeController run before Application_Start() in Global.asax.cs?

your connection string says you have a mdf file that should be attached to sqlserver, then set the default database to the one in the mdf. if you deleted your mfd file (rather then delete tables in database), then you will need to create a new database (name
should match connection string catalog name) with sqlserver,  detach it and copy the new mfd to the desired location.

you can change your project to create database rather than attach. in this case edit the data connection used.

I created MyStore.Models.StoreEntities.mdf in App_Data folder and now it’s working.

Thank you.

Leave a Reply