The database is created successfully (like tables), but not seeded. I spent several hours and read many articles, but could not get them. Any suggestions?
On the other hand, is it possible to call the initializer without reference to my DatabaseContext on the client?
I included all the relevant code that I could think of. If anything else is helpful, please let me know.
Things I tried:
- I deleted the connection string (since it defaults to sqlexpress, only the name is changed)
- I changed DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways, all the same.
Edit: It is really strange that this worked once, but I have no idea how and why it broke again. I assume connection strings, but who knows.
DatabaseInitializer.cs
public class DatabaseInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext> { protected override void Seed(DatabaseContext context) {
DatabaseContext.cs
public class DatabaseContext : DbContext { protected override void OnModelCreating(DbModelBuilder mb) {
Global.asax.cs - Application_Start ()
protected void Application_Start() { Database.SetInitializer<DatabaseContext>(new DatabaseInitializer()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
Web.config client
<connectionStrings> <add name="DatabaseContext" connectionString="data source=.\SQLEXPRESS;Database=Database;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /> </connectionStrings>
DECISION
For documentation, I shared my solution here. In any case, moving all the comments would be a pain. In the end, I had DatabaseInitializer and DatabaseContext in separate classes. I do not understand, although these tiny changes corrected him, but here it is.
DatabaseInitializer.cs
public class DatabaseInitializer : CreateDatabaseIfNotExists<DatabaseContext> { protected override void Seed(DatabaseContext context) {
DatabaseContext.cs
public class DatabaseContext : DbContext { public DatabaseContext() : base("MyDatabase") { } protected override void OnModelCreating(DbModelBuilder mb) {
Global.asax.cs - Application_Start ()
protected void Application_Start() { Database.SetInitializer(new DatabaseInitializer()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
entity framework asp.net-mvc-3 entity-framework code-first
OpticalDelusion Jun 10 2018-11-18T00: 00Z
source share