Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 1.14 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ObjectContext.CreateDatabaseScript not creating indexes
  2. public class CreateTablesIfNotExist<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
  3. {
  4.     public void InitializeDatabase(TContext context)
  5.     {
  6.         bool databaseExists;
  7.  
  8.         using (new TransactionScope(TransactionScopeOption.Suppress))
  9.         {
  10.             databaseExists = context.Database.Exists();
  11.         }
  12.  
  13.         if (databaseExists)
  14.         {
  15.             // Check to see if tables are already created
  16.             int numberOfTables = 0;
  17.             foreach (var t1 in context.Database.SqlQuery<int>("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_type = 'BASE TABLE' "))
  18.                 numberOfTables = t1;
  19.  
  20.             if (numberOfTables == 0)
  21.             {
  22.                 // Create all database tables
  23.                 var dbCreationScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
  24.                 context.Database.ExecuteSqlCommand(dbCreationScript);
  25.                 context.SaveChanges();
  26.             }
  27.         }
  28.         else
  29.         {
  30.             throw new ApplicationException("No database instance");
  31.         }
  32.     }
  33. }