1

I am wondering if there is some way to debug the Database generation of a ASP.NET MVC 3 application. I am encountering some problem: the database is not created and I have no error message.

UPDATED

Here's my connection string:

  <connectionStrings>
    <add name="SmartRent.Models.AdContext"  connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Here is my Global.asax.cs

protected void Application_Start()
{
    System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseAlways<SmartRent.Models.AdContext>());
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

Here is my EF DataContext:

namespace SmartRent.Models
{
    public class AdContext : DbContext
    {
        public DbSet<Ad> Ads { get; set; }

        public DbSet<CraneLocation> CraneLocations { get; set; }
    }
}
Angelo Badellino
  • 2,141
  • 2
  • 24
  • 45
  • What kind of exception? I had random problem with migration with unexpected error. It solves by referencing -StartUpProjectName flag while migration – Denis Agarev Mar 16 '12 at 13:38
  • There is no exception. The web application simply start but the database is not created. – Angelo Badellino Mar 16 '12 at 13:57
  • You should run Update-database command in Package manager console to create it. It has no connects with compilation – Denis Agarev Mar 16 '12 at 13:58
  • Are you sure Denis? Where I have to fire that command? – Angelo Badellino Mar 16 '12 at 17:11
  • this looks funny to me: connectionString="Data Source=.\SQLEXPRESS; -someone more expert can say, can you ever just connect to sqlexpress with no instance name? – Levin Magruder Mar 16 '12 at 17:23
  • @AngeloBad, have you got a database already created that you're referencing? WHen I set up database first I used the quick steps here, which are focused on how to get another package set up, but it might be of use: http://weblogs.asp.net/paullitwin/archive/2011/08/11/use-mvc-scaffolding-in-database-first-mode.aspx – Levin Magruder Mar 16 '12 at 17:26
  • @LevinMagruder it sounds funny but EF can't work without connectionstring at all if Visual Studio works under .SQLExpress admin – Denis Agarev Mar 16 '12 at 17:41
  • Levin open VS -> View -> Other Windows -> Package Manager Console. Read this article to understand creating/migrating concepts: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx – Denis Agarev Mar 16 '12 at 22:29
  • >>I am wondering if there is some way to debug the Database generation of a ASP.NET MVC 3 application. MVC has nothing to do with the question. A MVC app using Oracale and ODBC? Access? This is a trivial EF problem. Start with simple working code and compare to your failing code. It's easy to install run the SQL profiler. Compare the working/failing code. – RickAndMSFT Mar 22 '12 at 17:18

3 Answers3

1
  1. Run my completed soln of my tutorial http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part1-cs
  2. My code works, so modify my code/your code until it breaks/works.

The best way to solve this type of problem is to use the SQL profiler. Try to repro first with my code.

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
1

From the Code First perspective alone...
this might help you Entity Framework 4.3 doesn't create database
and you have to work with PM console, there is no other way to get around problems (and that's the best way to actually 'debug' what's going on).
Each step in PM console has to result in success - and no exceptions, also read the comments as they often tell you what might be wrong.
You could also run the Update-Database -Script to get a sense what is being created (before it's actually created).
And SQLExpress is default (I think), so you can drop the connection string to test things w/o specifying the connection.
In short, some consistency with around this and playing/understanding the PM console, helped me get control of things, might help you too.
Also manually removing previous Db-s created (by Code First if you have any at all), removing migrations if existing, recompiling and then re-running, so the usual debugging techniques:).

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

See: Debugging Package Manager Console Update-Database Seed Method

Clipped from the answer

if (System.Diagnostics.Debugger.IsAttached == false)
  System.Diagnostics.Debugger.Launch();
Community
  • 1
  • 1
David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67