Jeroen Vannevel
Jeroen Vannevel

Reputation: 44448

Database isn't generated from model

I'm trying to generate a database from a model. I believe I've setup everything I can find in other questions/walktroughs, but my database doesn't seem to be generated.

Context:

public class DatabaseContext : DbContext {
    public DatabaseContext()
        : base("Name=DatabaseContext") {
    }

    public DbSet<Show> Shows { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Configurations.Add(new ShowMap());
    }
}

Mapping:

public class ShowMap : EntityTypeConfiguration<Show> {
    public ShowMap() {
        ToTable("Shows");

        // Key
        // Properties
    }
}

Initializer:

public class DatabaseInitializer : DropCreateDatabaseAlways<DatabaseContext> {
    private List<Show> _shows = new List<Show>();

    protected override void Seed(DatabaseContext context) {
        LoadShows();
        _shows.ForEach(s => context.Shows.Add(s));
    }

    private void LoadShows() {
        // Creating models
    }
}

Global.asax:

Database.SetInitializer(new DatabaseInitializer());

Web.config:

<add name="DatabaseContext" connectionString="Data Source=JEROEN-DESKTOP\SQLEXPRESS;Initial Catalog=NoName;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

I have manually created the schema 'NoName' without adding tables. Executing my program goes without problems, but no table is generated. When I look at the connections in the VS SQL server object explorer I can see both localdb and SQLEXPRESS don't contain any relevant tables.

Another thing to note: I have added EntityFramework to my project using nuget, but when I rightclick on any file/folder, I don't see EntityFramework in the context menu. Could this be the issue? I've removed and added EF to the project, but it's still not showing up. There's an 'EntityFramework' entry in my References folder though.

Upvotes: 1

Views: 103

Answers (1)

ckross01
ckross01

Reputation: 1671

I would start looking at a few things, permissions being one. Make sure the delegating caller can actually create on this database. Also validate that EF is access the database during debugging : base("name=DatabaseContext"). If you can access the database, IntelliTrace will give you the out put statements it is creating.

Also, I would validate migrations is enabled. Using Nuget Package Manager Console run, Enable-Migrations.The full tutorial is Building an Initial Model & Database

Upvotes: 2

Related Questions