public void Create_creates_valid_EntityConnection_and_returns_EF_version()
        {
            var mockProviderServices = SetupMockProviderServices();
            mockProviderServices
                .Protected()
                .Setup<DbProviderManifest>("GetDbProviderManifest", ItExpr.IsAny<string>())
                .Returns(SqlProviderServices.Instance.GetProviderManifest("2008"));

            var mockResolver = SetupMockResolver(mockProviderServices);

            foreach (var targetEFVersion in EntityFrameworkVersion.GetAllVersions())
            {
                Version actualEFVersion;

                var entityConnection =
                    new StoreSchemaConnectionFactory().Create(
                        mockResolver.Object,
                        "System.Data.SqlClient",
                        "Server=test",
                        targetEFVersion,
                        out actualEFVersion);

                Assert.NotNull(entityConnection);
                var expectedVersion =
                    targetEFVersion == EntityFrameworkVersion.Version2
                        ? EntityFrameworkVersion.Version1
                        : targetEFVersion;

                Assert.Equal(expectedVersion, actualEFVersion);
            }
        }
Exemplo n.º 2
0
        public void Create_creates_valid_EntityConnection_and_returns_EF_version()
        {
            var mockProviderServices = SetupMockProviderServices();

            mockProviderServices
            .Protected()
            .Setup <DbProviderManifest>("GetDbProviderManifest", ItExpr.IsAny <string>())
            .Returns(SqlProviderServices.Instance.GetProviderManifest("2008"));

            var mockResolver = SetupMockResolver(mockProviderServices);

            foreach (var targetEFVersion in EntityFrameworkVersion.GetAllVersions())
            {
                Version actualEFVersion;

                var entityConnection =
                    new StoreSchemaConnectionFactory().Create(
                        mockResolver.Object,
                        "System.Data.SqlClient",
                        "Server=test",
                        targetEFVersion,
                        out actualEFVersion);

                Assert.NotNull(entityConnection);
                var expectedVersion =
                    targetEFVersion == EntityFrameworkVersion.Version2
                        ? EntityFrameworkVersion.Version1
                        : targetEFVersion;

                Assert.Equal(expectedVersion, actualEFVersion);
            }
        }
Exemplo n.º 3
0
        public void IsCatchableExceptionType_filters_exceptions_correctly()
        {
            Assert.False(StoreSchemaConnectionFactory.IsCatchableExceptionType(new StackOverflowException()));
            Assert.False(StoreSchemaConnectionFactory.IsCatchableExceptionType(new OutOfMemoryException()));
            Assert.False(StoreSchemaConnectionFactory.IsCatchableExceptionType(new NullReferenceException()));
            Assert.False(StoreSchemaConnectionFactory.IsCatchableExceptionType(new AccessViolationException()));
            Assert.False(StoreSchemaConnectionFactory.IsCatchableExceptionType(new SecurityException()));

            Assert.True(StoreSchemaConnectionFactory.IsCatchableExceptionType(new Exception()));
            Assert.True(StoreSchemaConnectionFactory.IsCatchableExceptionType(new InvalidOperationException()));
        }
Exemplo n.º 4
0
        // internal virtual for testing
        internal virtual StoreSchemaDetails GetStoreSchemaDetails(StoreSchemaConnectionFactory connectionFactory)
        {
            Version storeSchemaModelVersion;
            var connection =
                connectionFactory
                    .Create(
                        DependencyResolver.Instance,
                        _settings.RuntimeProviderInvariantName,
                        _settings.DesignTimeConnectionString,
                        _settings.TargetSchemaVersion,
                        out storeSchemaModelVersion);

            var facadeFilters =
                _settings.DatabaseObjectFilters ?? Enumerable.Empty<EntityStoreSchemaFilterEntry>();

            return
                CreateDbSchemaLoader(connection, storeSchemaModelVersion)
                    .LoadStoreSchemaDetails(facadeFilters.ToList());
        }
 public bool InvokeCanCreateAndOpenConnection(StoreSchemaConnectionFactory connectionFactory, 
     string providerInvariantName, string designTimeInvariantName, string designTimeConnectionString)
 {
     return CanCreateAndOpenConnection(
         connectionFactory, providerInvariantName, designTimeInvariantName, designTimeConnectionString);
 }
        protected static bool CanCreateAndOpenConnection(
            StoreSchemaConnectionFactory connectionFactory, string providerInvariantName, string designTimeInvariantName,
            string designTimeConnectionString)
        {
            Debug.Assert(connectionFactory != null, "connectionFactory != null");
            Debug.Assert(
                !string.IsNullOrWhiteSpace(designTimeInvariantName),
                "designTimeInvariantName must not be null or empty");
            Debug.Assert(
                !string.IsNullOrWhiteSpace(designTimeConnectionString),
                "designTimeConnectionString must not be null or empty");

            EntityConnection entityConnection = null;
            try
            {
                // attempt to create a DbConnection using the provider connection string we have. This will
                // throw an exception if the connection cannot be made, for example, if the credentials aren't
                // set. This has to be done using DDEX-based APIs since the SchemaGenerator is based off of
                // DbConnection, and DDEX will save the password whereas DbConnection will not.
                Version _;
                entityConnection = connectionFactory.Create(
                    DependencyResolver.Instance,
                    providerInvariantName,
                    designTimeConnectionString,
                    EntityFrameworkVersion.Latest,
                    out _);
                entityConnection.Open();
            }
            catch
            {
                return false;
            }
            finally
            {
                // Close the EntityConnection
                if (entityConnection != null)
                {
                    VsUtils.SafeCloseDbConnection(entityConnection, designTimeInvariantName, designTimeConnectionString);
                }
            }

            return true;
        }
        private static ICollection<EntityStoreSchemaFilterEntry> ExecuteDatabaseMetadataQuery(
            string esqlQuery, EntityStoreSchemaFilterObjectTypes types, ModelBuilderSettings settings, DoWorkEventArgs args)
        {
            var filterEntries = new List<EntityStoreSchemaFilterEntry>();

            EntityConnection ec = null;
            try
            {
                Version actualEntityFrameworkConnectionVersion;
                ec = new StoreSchemaConnectionFactory().Create(
                    DependencyResolver.Instance,
                    settings.RuntimeProviderInvariantName,
                    settings.DesignTimeConnectionString,
                    settings.TargetSchemaVersion,
                    out actualEntityFrameworkConnectionVersion);

                // if the provider does not support V3 and we are querying for Functions then switch to the pre-V3 query
                if (actualEntityFrameworkConnectionVersion < EntityFrameworkVersion.Version3
                    && SelectFunctionsESqlQuery.Equals(esqlQuery, StringComparison.Ordinal))
                {
                    esqlQuery = SelectFunctionsESqlQueryBeforeV3;
                }

                using (var command = new EntityCommand(null, ec, DependencyResolver.Instance))
                {
                    // NOTE:  DO NOT set the the command.CommandTimeout value.  Some providers don't support a non-zero value, and will throw (eg, SqlCE provider). 
                    // The System.Data.SqlClient's default value is 15, so we will still get a timeout for sql server. 

                    command.CommandType = CommandType.Text;
                    command.CommandText = esqlQuery;
                    ec.Open();
                    DbDataReader reader = null;
                    try
                    {
                        reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
                        while (reader.Read())
                        {
                            if (args != null
                                && args.Cancel)
                            {
                                break;
                            }

                            if (reader.FieldCount == 3)
                            {
                                // the types coming back through the reader may not be a string 
                                // (eg, SqlCE returns System.DbNull for catalogName & schemaName), so cast carefully
                                var catalogName = reader.GetValue(0) as String;
                                var schemaName = reader.GetValue(1) as String;
                                var name = reader.GetValue(2) as String;

                                if (String.IsNullOrEmpty(name) == false)
                                {
                                    filterEntries.Add(
                                        new EntityStoreSchemaFilterEntry(
                                            catalogName, schemaName, name, types, EntityStoreSchemaFilterEffect.Allow));
                                }
                            }
                            else
                            {
                                Debug.Fail("Unexpected field count in reader");
                            }
                        }
                    }
                    finally
                    {
                        if (reader != null)
                        {
                            try
                            {
                                reader.Close();
                                reader.Dispose();
                            }
                            catch (Exception)
                            {
                                Debug.Fail(
                                    "Could not close the DbDataReader in ExecuteDatabaseMetadataQuery(). If this is the result of a connection to a database file, it will leave a read lock on the file.");
                            }
                        }
                    }
                }
            }
            finally
            {
                if (ec != null)
                {
                    try
                    {
                        ec.Close();
                        ec.Dispose();
                    }
                    catch (Exception)
                    {
                        Debug.Fail(
                            "Could not close the EntityConnection in ExecuteDatabaseMetadataQuery(). If this is a connection to a database file, it will leave a read lock on the file.");
                    }
                }
            }

            return filterEntries;
        }