Beispiel #1
0
 public DatabaseExistenceState AnyModelTableExists(
     InternalContext internalContext)
 {
     if (!internalContext.DatabaseOperations.Exists(internalContext.Connection, internalContext.CommandTimeout, new Lazy <StoreItemCollection>((Func <StoreItemCollection>)(() => DatabaseTableChecker.CreateStoreItemCollection(internalContext)))))
     {
         return(DatabaseExistenceState.DoesNotExist);
     }
     using (ClonedObjectContext contextForDdlOps = internalContext.CreateObjectContextForDdlOps())
     {
         try
         {
             if (internalContext.CodeFirstModel == null)
             {
                 return(DatabaseExistenceState.Exists);
             }
             TableExistenceChecker service = DbConfiguration.DependencyResolver.GetService <TableExistenceChecker>((object)internalContext.ProviderName);
             if (service == null)
             {
                 return(DatabaseExistenceState.Exists);
             }
             List <EntitySet> list = this.GetModelTables(internalContext).ToList <EntitySet>();
             if (!list.Any <EntitySet>() || this.QueryForTableExistence(service, contextForDdlOps, list))
             {
                 return(DatabaseExistenceState.Exists);
             }
             return(internalContext.HasHistoryTableEntry() ? DatabaseExistenceState.Exists : DatabaseExistenceState.ExistsConsideredEmpty);
         }
         catch (Exception ex)
         {
             return(DatabaseExistenceState.Exists);
         }
     }
 }
        public DatabaseExistenceState AnyModelTableExists(InternalContext internalContext)
        {
            var exists = internalContext.DatabaseOperations.Exists(
                internalContext.Connection,
                internalContext.CommandTimeout,
                new Lazy <StoreItemCollection>(() => CreateStoreItemCollection(internalContext)));

            if (!exists)
            {
                return(DatabaseExistenceState.DoesNotExist);
            }

            using (var clonedObjectContext = internalContext.CreateObjectContextForDdlOps())
            {
                try
                {
                    if (internalContext.CodeFirstModel == null)
                    {
                        // If not Code First, then assume tables created in some other way
                        return(DatabaseExistenceState.Exists);
                    }

                    var checker = DbConfiguration.DependencyResolver.GetService <TableExistenceChecker>(internalContext.ProviderName);

                    if (checker == null)
                    {
                        // If we can't check for tables, then assume they exist as we did in older versions
                        return(DatabaseExistenceState.Exists);
                    }

                    var modelTables = GetModelTables(internalContext).ToList();

                    if (!modelTables.Any())
                    {
                        // If this is an empty model, then all tables that can exist (0) do exist
                        return(DatabaseExistenceState.Exists);
                    }

                    if (QueryForTableExistence(checker, clonedObjectContext, modelTables))
                    {
                        // If any table exists, then assume that this is a non-empty database
                        return(DatabaseExistenceState.Exists);
                    }

                    // At this point we know no model tables exist. If the history table exists and has an entry
                    // for this context, then treat this as a non-empty database, otherwise treat is as existing
                    // but empty.
                    return(internalContext.HasHistoryTableEntry()
                        ? DatabaseExistenceState.Exists
                        : DatabaseExistenceState.ExistsConsideredEmpty);
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.Message, ex.ToString());

                    // Revert to previous behavior on error
                    return(DatabaseExistenceState.Exists);
                }
            }
        }
 private static StoreItemCollection CreateStoreItemCollection(InternalContext internalContext)
 {
     using (var clonedObjectContext = internalContext.CreateObjectContextForDdlOps())
     {
         var entityConnection = ((EntityConnection)clonedObjectContext.ObjectContext.Connection);
         return((StoreItemCollection)entityConnection.GetMetadataWorkspace().GetItemCollection(DataSpace.SSpace));
     }
 }
Beispiel #4
0
 private static StoreItemCollection CreateStoreItemCollection(
     InternalContext internalContext)
 {
     using (ClonedObjectContext contextForDdlOps = internalContext.CreateObjectContextForDdlOps())
         return((StoreItemCollection)(EntityConnection)contextForDdlOps.ObjectContext.Connection.GetMetadataWorkspace().GetItemCollection(DataSpace.SSpace));
 }
Beispiel #5
0
        public bool AnyModelTableExists(InternalContext internalContext)
        {
            using (var clonedObjectContext = internalContext.CreateObjectContextForDdlOps())
            {
                var exists = internalContext.DatabaseOperations.Exists(clonedObjectContext.ObjectContext);

                if (!exists)
                {
                    return(false);
                }

                try
                {
                    if (internalContext.CodeFirstModel == null)
                    {
                        return(true);
                    }

                    var             providerName = internalContext.ProviderName;
                    IPseudoProvider provider;

                    switch (providerName)
                    {
                    case "System.Data.SqlClient":
                        provider = new SqlPseudoProvider();
                        break;

                    case "System.Data.SqlServerCe.4.0":
                        provider = new SqlCePseudoProvider();
                        break;

                    default:
                        return(true);
                    }

                    var modelTables = GetModelTables(internalContext.ObjectContext.MetadataWorkspace).ToList();

                    if (!modelTables.Any())
                    {
                        return(true);
                    }

                    using (new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        if (provider.AnyModelTableExistsInDatabase(
                                clonedObjectContext.ObjectContext, clonedObjectContext.Connection, modelTables,
                                EdmMetadataContext.TableName))
                        {
                            return(true);
                        }
                    }

                    return(internalContext.HasHistoryTableEntry());
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.Message, ex.ToString());

                    // Revert to previous behavior on error
                    return(true);
                }
            }
        }
Beispiel #6
0
        public DatabaseExistenceState AnyModelTableExists(InternalContext internalContext)
        {
            using (var clonedObjectContext = internalContext.CreateObjectContextForDdlOps())
            {
                var exists = internalContext.DatabaseOperations.Exists(clonedObjectContext.ObjectContext);

                if (!exists)
                {
                    return(DatabaseExistenceState.DoesNotExist);
                }

                try
                {
                    if (internalContext.CodeFirstModel == null)
                    {
                        // If not Code First, then assume tables created in some other way
                        return(DatabaseExistenceState.Exists);
                    }

                    var             providerName = internalContext.ProviderName;
                    IPseudoProvider provider;

                    switch (providerName)
                    {
                    case "System.Data.SqlClient":
                        provider = new SqlPseudoProvider();
                        break;

                    case "System.Data.SqlServerCe.4.0":
                        provider = new SqlCePseudoProvider();
                        break;

                    default:
                        // If we can't check for tables, then assume they exist as we did in older versions
                        return(DatabaseExistenceState.Exists);
                    }

                    var modelTables = GetModelTables(internalContext).ToList();

                    if (!modelTables.Any())
                    {
                        // If this is an empty model, then all tables that can exist (0) do exist
                        return(DatabaseExistenceState.Exists);
                    }

                    if (QueryForTableExistence(provider, clonedObjectContext, modelTables))
                    {
                        // If any table exists, then assume that this is a non-empty database
                        return(DatabaseExistenceState.Exists);
                    }

                    // At this point we know no model tables exist. If the history table exists and has an entry
                    // for this context, then treat this as a non-empty database, otherwise treat is as existing
                    // but empty.
                    return(internalContext.HasHistoryTableEntry()
                               ? DatabaseExistenceState.Exists
                               : DatabaseExistenceState.ExistsConsideredEmpty);
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.Message, ex.ToString());

                    // Revert to previous behavior on error
                    return(DatabaseExistenceState.Exists);
                }
            }
        }