Question

Working with Code First feature of Entity Framework 4.1 using an ASP.NET MVC 3 project.

However the database (SQL Server 2008 R2) does not automatically create the table mapping on application startup. Any ideas on how to make it do so?

using System;
using System.Data.Entity;
namespace MvcMovie.Models { 
public class Movie { 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public DateTime ReleaseDate { get; set; } 
    public string Genre { get; set; } 
    public decimal Price { get; set; } 
}

Also there is this database context class

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
} 
}

In global.asax file

protected void Application_Start()
{
        AreaRegistration.RegisterAllAreas();

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

Kindly help?? I am a beginner? Even you could suggest some reading material for asp.net it would be of great help ?

Was it helpful?

Solution

Here is a good overview. You can use a config entry:

<entityFramework>
  <contexts>
    <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
      <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />
    </context>
  </contexts>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>

Or set it up via code as mentioned here:

Database.SetInitializer(new DropCreateDatabaseAlways<BlogContext>());
using (var db = new BlogContext())
{
    db.Database.Initialize(false);
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top