I’m having problems trying to get my database to seed with entity framework / code first. I’m a beginner at .NET and I’ve tried numerous of solutions but none of them worked for me.
What am I doing wrong? I’m posting the most relevant code… Any help would be greatly appreciated.
SampleData.cs
namespace seed2.Models
{
public class SampleData : DropCreateDatabaseAlways<AirplaneDB>
{
protected override void Seed(AirplaneDB context)
{
var airplane = new List<Airplane>()
{
new Airplane { AirplaneId = 1, Make = "Great Planes"},
new Airplane { AirplaneId = 2, Make = "Eflite"}
};
base.Seed(context);
}
}
}
Web.Config
<add name="AirplaneDB"
connectionString="data source=|DataDirectory|Airplanes.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
AirplaneDB.cs (Context)
public class AirplaneDB : DbContext
{
public DbSet<Airplane> Airplanes { get; set; }
}
Airplane.cs
public class Airplane
{
public int AirplaneId { get; set; }
public string Make { get; set; }
}
First, add this to the Application_Start() method of Global.asax.cs:
Make sure you are actually refreshing the view in the browser. Re-building the solution doesn’t seed the database.
Finally, you should add
airplane.ForEach(x => context.Airplanes.Add(x));and removebase.Seed(context)in your seed method.