I am starting a ASP.NET Webforms application in Visual Studio 2015 and I am trying to add my models to a database using Entity Framework Code First but the database file is not showing up. I am using the SQL Server Express LocalDB that comes when installing VS for simplicity. I have the following code set up to create and populate my model.
The model class "Monster":
using System.ComponentModel.DataAnnotations;
namespace MonsterMadness.Models
{
public class Monster
{
[ScaffoldColumn(false)]
public int MonsterId { get; set; }
public string Name { get; set; }
[ScaffoldColumn(false)]
public int AttackPower { get; set; }
[ScaffoldColumn(false)]
public int Defence { get; set; }
[ScaffoldColumn(false)]
public int ChanceToHit { get; set; }
[ScaffoldColumn(false)]
public int Dodge { get; set; }
[ScaffoldColumn(false)]
public int Critical { get; set; }
}
}
My DB context class "MonsterMadnessContext":
using System.Data.Entity;
namespace MonsterMadness.Models
{
public class MonsterMadnessContext : DbContext
{
public MonsterMadnessContext() : base("MonsterMadness")
{
}
public DbSet<Monster> Monsters
{
get; set;
}
}
}
My initializer "MonsterMadnessDatabaseInitializer":
using System.Data.Entity;
namespace MonsterMadness.Models
{
public class MonsterMadnessDatabaseInitializer : DropCreateDatabaseIfModelChanges<MonsterMadnessContext>
{
protected override void Seed(MonsterMadnessContext context)
{
GetMonsters().ForEach(monster => context.Monsters.Add(monster));
}
private static List<Monster> GetMonsters()
{
List<Monster> monsters = new List<Monster>
{
new Monster()
{
Name = "Test 1",
AttackPower = 10,
ChanceToHit = 10,
Critical = 10,
Defence = 10,
Dodge = 10
},
new Monster()
{
Name = "Test 2",
AttackPower = 20,
ChanceToHit = 20,
Critical = 20,
Defence = 20,
Dodge = 20
}
};
return monsters;
}
}
}
I have my initializer being run in the Global.ascx file:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//Initialize database
Database.SetInitializer(new MonsterMadnessDatabaseInitializer());
}
}
And lastly my web.config entry for the "MonsterMadess" connection string which should be pointing to LocalDB:
<connectionStrings>
<add name="MonsterMadness" connectionString="Data Source=(LocalDB)\v12.0;AttachDbFilename=|DataDirectory|\monstermadness.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I set up everything based on this tutorial but whenever I run the application I do not see the mdf file being created. Since I have an initializer being run there should be data in the table and the file should be created. What am I missing? Thanks for the help.
Add this line into context constructor :
Database.SetInitializer(
new CreateDatabaseIfNotExists<MonsterMadnessContext>());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.