Entity Framework CTP 4 - Code First Custom Database Initializer

后端 未结 6 666
傲寒
傲寒 2020-11-28 23:53

I would like to implement a custom database initialization strategy so that I can generate the database schema and apply it to an EXISTING EMPTY SQL database using a supplie

6条回答
  •  孤街浪徒
    2020-11-29 00:26

    I too was looking for a good solution since godaddy does not allow drop/creation of database and thus no tables created. Since the newer version of Entity Framework has obsoleted EDMData, I modified Alex's code to see if a DropMeToRecreateDatabase table exists or not, if it doesnt exist, it deletes all tables and recreates new tables.

    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    using System.Linq;
    
    namespace LadyTreble.DatabaseInitializer
    {
    
        public class DontDropExistingDbCreateTablesIfTableDropped : IDatabaseInitializer where T : DbContext
        {
            public bool TryInitializeDatabase(T context)
            {
                var objectContext = ((IObjectContextAdapter)context).ObjectContext;
                if (objectContext.ExecuteStoreQuery(GetTableCount).FirstOrDefault() == 0)
                {
                    this.DeleteExistingTables(objectContext);
                    this.CreateTables(objectContext);
                }
                return true;
            }
    
            public void InitializeDatabase(T context)
            {
                this.TryInitializeDatabase(context);
            }
    
            private void CreateTables(ObjectContext objectContext)
            {
                string dataBaseCreateScript = objectContext.CreateDatabaseScript();
                objectContext.ExecuteStoreCommand(dataBaseCreateScript);
            }
    
            private void DeleteExistingTables(ObjectContext objectContext)
            {
    
                objectContext.ExecuteStoreCommand(DeleteAllTablesScript);
            }
    
            private const string DeleteAllTablesScript =
                @"declare @cmd varchar(4000)
    
                  DECLARE cmds0 CURSOR FOR 
                  SELECT 'ALTER TABLE ' + TABLE_NAME + ' DROP CONSTRAINT ' + CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
    
                  DECLARE cmds1 CURSOR FOR 
                  SELECT 'ALTER TABLE ' + TABLE_NAME + ' DROP CONSTRAINT ' + CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
    
                  DECLARE cmds2 CURSOR FOR 
                  SELECT 'TRUNCATE TABLE ' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
    
                  DECLARE cmds3 CURSOR FOR 
                  SELECT 'DROP TABLE [' + TABLE_NAME + ']' FROM INFORMATION_SCHEMA.TABLES
    
                  open cmds0
                  while 1=1
                  begin
                      fetch cmds0 into @cmd
                      if @@fetch_status != 0 break
                      print @cmd
                      exec(@cmd)
                  end
                  close cmds0
                  deallocate cmds0
    
                  open cmds1
                  while 1=1
                  begin
                      fetch cmds1 into @cmd
                      if @@fetch_status != 0 break
                      print @cmd
                      exec(@cmd)
                  end
                  close cmds1
                  deallocate cmds1
    
                  open cmds2
                  while 1=1
                  begin
                      fetch cmds2 into @cmd
                      if @@fetch_status != 0 break
                      print @cmd
                      exec(@cmd)
                  end
                  close cmds2
                  deallocate cmds2
    
                  open cmds3
                  while 1=1
                  begin
                      fetch cmds3 into @cmd
                      if @@fetch_status != 0 break
                      print @cmd
                      exec(@cmd)
                  end
                  close cmds3
                  deallocate cmds3
    
                 CREATE TABLE DropMeToRecreateDatabase(id int IDENTITY(1,1) NOT NULL)";
    
            private const string GetTableCount =
                @"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES T WHERE T.TABLE_NAME = 'DropMeToRecreateDatabase'";
        }
    
    }
    

提交回复
热议问题