3

I've got a problem with creating a new database every time my application starts.

I read this post Entity Framework Database.SetInitializer simply not working , but it didn't help.

Here is my code:

public DatabaseContext(DatabaseType type) : base("name=" + type)
{
   Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext>());
}

and I'm calling

Database.Initialize(true);

doesn't matter if true or false, both doesn't work.

Later in my application I also "work" with the database so the initializer should definitely be called.

I open the context via "using". And even if I do a transaction like this:

database.RoleRepository.Insert(new Role());
database.Save();

it doesn't work.

The funny thing is that it works with LocalDB but not with Compact Edition or SQL Server Express. I work with all three databases inside the same application (the project requires this), maybe that's the mistake? LocalDB is called first.

Here are my connections strings:

<connectionStrings>
    <add name="LocalDB" 
         connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Database.DatabaseContext;integrated security=True;MultipleActiveResultSets=True" 
         providerName="System.Data.SqlClient"/>
    <add name="CompactEdition" 
         connectionString="Data Source=CompactEditionDB.sdf" 
         providerName="System.Data.SqlServerCe.4.0"/>
    <add name="SQLExpress" 
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SQLExpressDatabase;User Instance=false;MultipleActiveResultSets=true;Integrated Security=SSPI" 
         providerName="System.Data.SQLClient"/>
</connectionStrings>

Do you know why it only works with LocalDB?

EDIT:

Found out that it only works with the first context/database, doesn't matter which type. Both methods

Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext>());
Database.Initialize(true);

are called, but it looks like as if they are not executed. As already mentioned they all have the same context but consist of different objects.

Why is only the first database recreated?

Community
  • 1
  • 1
0lli.rocks
  • 1,027
  • 1
  • 18
  • 31
  • I found out that only the first DB is recreated, the other two are still the same. They have the same DBContext but consist of different objects and every object calls the Init method. – 0lli.rocks Jul 22 '14 at 14:30
  • Can you edit your post to show what strings you pass in to your DatabaseContext constructor? These need to match the name of your connection strings. – Daniel Simpkins Jul 22 '14 at 14:39
  • I noticed that already, if one of these databases don't exists they are created -> connection strings work. I also can work with the databases, everything works except from recreating every time when the application starts. A bad workaround would be to delete the files manually before calling the context... – 0lli.rocks Jul 22 '14 at 14:43
  • any error message ? The provider name for SQLExpress is not correct in terms of case SqlClient, not SQLClient. Don't know if it matters. – tschmit007 Jul 22 '14 at 15:54
  • no error message, as i mentioned before, the connection strings are working, the databases are created. After some research I found out that the problem is the Initializer, I call it for each database but it executes only for the first. I don't want to create a own DbContext for each database. – 0lli.rocks Jul 22 '14 at 16:39

0 Answers0