public void EntityCommandDefinition_is_executed_with_correct_EntityCommand_and_CommandBehavior() { var entityConnection = MockHelper.CreateEntityConnection(); var passedEntityCommand = default(EntityCommand); var passedCommandbehavior = default(CommandBehavior); var storeDataReader = new Mock <DbDataReader>().Object; var entityCommandDefinitionMock = new Mock <EntityCommandDefinition>(MockBehavior.Strict, null, null, null); entityCommandDefinitionMock.SetupGet(m => m.Parameters).Returns(Enumerable.Empty <EntityParameter>()); entityCommandDefinitionMock.Setup(m => m.Execute(It.IsAny <EntityCommand>(), It.IsAny <CommandBehavior>())). Returns(storeDataReader). Callback( (EntityCommand ec, CommandBehavior cb) => { passedEntityCommand = ec; passedCommandbehavior = cb; }); var entityCommand = new EntityCommand( entityConnection, entityCommandDefinitionMock.Object, new DbInterceptionContext()); var commandBehavior = CommandBehavior.SequentialAccess; entityCommand.ExecuteReader(commandBehavior); Assert.Same(entityCommand, passedEntityCommand); Assert.Equal(passedCommandbehavior, commandBehavior); entityCommandDefinitionMock.Verify(m => m.Execute(entityCommand, commandBehavior), Times.Once()); }
public void Exception_thrown_if_EntityConnection_is_not_set() { var entityCommand = new EntityCommand(string.Empty, default(EntityConnection)); Assert.Equal( Strings.EntityClient_NoConnectionForCommand, Assert.Throws <InvalidOperationException>(() => entityCommand.ExecuteReader(CommandBehavior.Default)).Message); }
public FunctionDetailsReader(EntityCommand command, Version storeSchemaModelVersion) { Debug.Assert(command != null, "command != null"); Debug.Assert(storeSchemaModelVersion != null, "storeSchemaModelVersion != null"); _rowViewFactoryMethod = storeSchemaModelVersion < EntityFrameworkVersion.Version3 ? (values) => new FunctionDetailsV1RowView(values) : (Func<object[], FunctionDetailsRowView>)((values) => new FunctionDetailsV3RowView(values)); _command = command; _reader = _command.ExecuteReader(CommandBehavior.SequentialAccess); }
public void Exception_thrown_if_EntityConnection_StoreProviderFactory_is_not_set() { var entityConnectionMock = new Mock <EntityConnection>(); entityConnectionMock.SetupGet(m => m.StoreProviderFactory).Returns(default(DbProviderFactory)); var entityConnection = entityConnectionMock.Object; var entityCommand = new EntityCommand(string.Empty, entityConnection); Assert.Equal( Strings.EntityClient_ConnectionStringNeededBeforeOperation, Assert.Throws <InvalidOperationException>(() => entityCommand.ExecuteReader(CommandBehavior.Default)).Message); }
public void Exception_thrown_if_EntityConnection_State_is_Broken() { var providerFactory = new Mock <DbProviderFactory>(MockBehavior.Strict).Object; var dbConnection = new Mock <DbConnection>().Object; var entityConnectionMock = new Mock <EntityConnection>(); entityConnectionMock.SetupGet(m => m.StoreProviderFactory).Returns(providerFactory); entityConnectionMock.SetupGet(m => m.StoreConnection).Returns(dbConnection); entityConnectionMock.SetupGet(m => m.State).Returns(ConnectionState.Broken); var entityConnection = entityConnectionMock.Object; var entityCommand = new EntityCommand(string.Empty, entityConnection); Assert.Equal( Strings.EntityClient_ExecutingOnClosedConnection(Strings.EntityClient_ConnectionStateBroken), Assert.Throws <InvalidOperationException>(() => entityCommand.ExecuteReader(CommandBehavior.Default)).Message); }
public void UserDefinedFunction() { using (EntityConnection conn = new EntityConnection("name=testEntities")) { conn.Open(); string query = @"SELECT e.FirstName AS Name FROM testEntities.Employees AS e WHERE testModel.Store.spFunc(e.Id, '') = 6"; using (EntityCommand cmd = new EntityCommand(query, conn)) { EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); Assert.IsTrue(reader.Read()); Assert.AreEqual("Scooby", reader[0]); } } }
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; }