Looks like you're forgetting to add the just created Category
to the DB table. If you don't add it to the context
's table, Entity Framework
won't see anything... So you must do something like this:
protected override void Seed(LibraryEntities context)
{
var categories = new List<Category>
{
new Category{Id=1, Name="Sci-fi"}
};
foreach(Category c in categories)
{
context.Categories.Add(c)
}
// Call the Save method in the Context
context.SaveChanges();
}
For the DataSource problem, try this modified connection string:
<add name="LibraryEntities"
connectionString="DataSource=|DataDirectory|R10491_library.sdf"
providerName="System.Data.SqlServerCe.4.0" />
In one of my projects I have this connection string:
<add name="FitnessCenterContext"
connectionString="DataSource=|DataDirectory|FitnessCenter.Model.FitnessCenterContext.sdf"
providerName="System.Data.SqlServerCe.4.0" />
Note above that the database name matches the namespace and Context name.
I also use Application_Start()
to call the SetInitializer
method in Global.asax.cs
file. I see that you're calling it inside Session_Start()
. Maybe this is the problem... Change your code to:
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new R10491.Models.SampleData());
}
You can also try calling the Initialize
method:
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new R10491.Models.SampleData());
using (var context = new LibraryEntities())
{
context.Database.Initialize(true);
}
}