问题描述:

I am using entity framework 6 with Web API. I have applied Enable-Migrations command in package manager console and Migration folder created with Configuration class.

I have executed Add-Migration -InitialCreate command initial migration source file created but up and down methods are empty.

so when I recreate the database no table is created except migrationhistory table. No table is created into database so my API cannot perform any POST, GET operation and getting below error message.

Exception type: System.Data.SqlClient.SqlException

Message : Invalid object name 'dbo.Template'.

Please assist me.

I have put my connection string in database.config in startup project not in Web.config of startup project reason behind this was I didn't want to start Web API when database name changes in Web.config. I have attached my Web.config snapshot.

MigrationHistory Table

Below is code for DBContext class.

public class TestDBContext : DbContext

{

public DbSet<SamlNode> SamlNodes { get; set; }

public DbSet<Template> Templates { get; set; }

private static string connectionString

{

get

{

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);

ConnectionStringsSection connectionString = (ConnectionStringsSection)config.GetSection("database");

if (connectionString != null && connectionString.ConnectionStrings["TestDBContext"] != null)

{

return connectionString.ConnectionStrings["TestDBContext"].ConnectionString;

}

}

}

public TestDBContext()

: base(connectionString)

{

Database.SetInitializer<TestDBContext>(new CreateDatabaseIfNotExists<TestDBContext>());

//Database.SetInitializer(new MigrateDatabaseToLatestVersion<TestDBContext, Migrations.Configuration>(connectionString));

}

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

}

}

Regards,

Rashi

相关阅读:
Top