private void Validate(int index, object value) { Check.NotNull <object>(value, nameof(value)); EntityParameter entityParameter = (EntityParameter)value; object obj = entityParameter.CompareExchangeParent((object)this, (object)null); if (obj != null) { if (this != obj) { throw new ArgumentException(Strings.EntityParameterContainedByAnotherCollection); } if (index != this.IndexOf(value)) { throw new ArgumentException(Strings.EntityParameterContainedByAnotherCollection); } } if (entityParameter.ParameterName.Length != 0) { return; } index = 1; string parameterName; do { parameterName = "Parameter" + index.ToString((IFormatProvider)CultureInfo.CurrentCulture); ++index; }while (-1 != this.IndexOf(parameterName)); entityParameter.ParameterName = parameterName; }
/// <summary> /// Add a EntityParameter with the given name and value to the collection /// </summary> /// <param name="parameterName">The name of the parameter to add</param> /// <param name="value">The value of the parameter to add</param> /// <returns>The index of the new parameter within the collection</returns> public EntityParameter AddWithValue(string parameterName, object value) { var param = new EntityParameter(); param.ParameterName = parameterName; param.Value = value; return Add(param); }
/// <summary> /// Add a EntityParameter with the given name and value to the collection /// </summary> /// <param name="parameterName"> The name of the parameter to add </param> /// <param name="value"> The value of the parameter to add </param> /// <returns> The index of the new parameter within the collection </returns> public EntityParameter AddWithValue(string parameterName, object value) { var param = new EntityParameter(); param.ParameterName = parameterName; param.Value = value; return(Add(param)); }
private void RemoveIndex(int index) { List <EntityParameter> innerList = this.InnerList; EntityParameter entityParameter = innerList[index]; innerList.RemoveAt(index); entityParameter.ResetParent(); }
/// <summary> /// Adds a value to the end of the <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" />. /// </summary> /// <returns> /// A <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object. /// </returns> /// <param name="parameterName">The name of the parameter.</param> /// <param name="value">The value to be added.</param> public EntityParameter AddWithValue(string parameterName, object value) { EntityParameter entityParameter = new EntityParameter(); entityParameter.ParameterName = parameterName; entityParameter.Value = value; return(this.Add(entityParameter)); }
private void Replace(int index, object newValue) { List <EntityParameter> innerList = this.InnerList; EntityParameterCollection.ValidateType(newValue); this.Validate(index, newValue); EntityParameter entityParameter = innerList[index]; innerList[index] = (EntityParameter)newValue; entityParameter.ResetParent(); }
private EntityParameter(EntityParameter source) : this() { source.CloneHelper(this); ICloneable cloneable = this._value as ICloneable; if (cloneable == null) { return; } this._value = cloneable.Clone(); }
private EntityParameter(EntityParameter source) : this() { DebugCheck.NotNull(source); source.CloneHelper(this); var cloneable = (_value as ICloneable); if (null != cloneable) { _value = cloneable.Clone(); } }
private void CloneHelper(EntityParameter destination) { destination._value = this._value; destination._direction = this._direction; destination._size = this._size; destination._sourceColumn = this._sourceColumn; destination._sourceVersion = this._sourceVersion; destination._sourceColumnNullMapping = this._sourceColumnNullMapping; destination._isNullable = this._isNullable; destination._parameterName = this._parameterName; destination._dbType = this._dbType; destination._edmType = this._edmType; destination._precision = this._precision; destination._scale = this._scale; }
/// <summary> /// Copies all the elements of the current <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> to the specified /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// starting at the specified destination index. /// </summary> /// <param name="array"> /// The <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> that is the destination of the elements copied from the current /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </param> /// <param name="index"> /// A 32-bit integer that represents the index in the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// at which copying starts. /// </param> public void CopyTo(EntityParameter[] array, int index) { CopyTo((Array)array, index); }
/// <summary> /// Adds an array of <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> values to the end of the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </summary> /// <param name="values"> /// The <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> values to add. /// </param> public void AddRange(EntityParameter[] values) { AddRange((Array)values); }
/// <summary> /// Clones this parameter object /// </summary> /// <returns>The new cloned object</returns> private void CloneHelper(EntityParameter destination) { destination._value = _value; destination._direction = _direction; destination._size = _size; destination._sourceColumn = _sourceColumn; destination._sourceVersion = _sourceVersion; destination._sourceColumnNullMapping = _sourceColumnNullMapping; destination._isNullable = _isNullable; destination._parameterName = _parameterName; destination._dbType = _dbType; destination._edmType = _edmType; destination._precision = _precision; destination._scale = _scale; }
private EntityParameter(EntityParameter source) : this() { Contract.Requires(source != null); source.CloneHelper(this); var cloneable = (_value as ICloneable); if (null != cloneable) { _value = cloneable.Clone(); } }
/// <summary> /// Add a EntityParameter to the collection /// </summary> /// <param name="value"> The parameter to add to the collection </param> /// <returns> The index of the new parameter within the collection </returns> public EntityParameter Add(EntityParameter value) { Add((object)value); return(value); }
/// <summary> /// Add a EntityParameter with the given value to the collection at a location indicated by the index /// </summary> /// <param name="index"> The index at which the parameter is to be inserted </param> /// <param name="value"> The value of the parameter </param> public void Insert(int index, EntityParameter value) { Insert(index, (object)value); }
/// <summary> /// Removes the specified <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> from the collection. /// </summary> /// <param name="value"> /// A <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object to remove from the collection. /// </param> /// <exception cref="T:System.InvalidCastException"> /// The parameter is not a <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" />. /// </exception> /// <exception cref="T:System.SystemException">The parameter does not exist in the collection. </exception> public void Remove(EntityParameter value) { Remove((object)value); }
private static int ValueSize(object value) { return(EntityParameter.ValueSizeCore(value)); }
public ActionResult EntitySQL(int id = 0) { string esqlQuery = @"SELECT VALUE customers FROM AdventureWorksEntities.Customers AS customers WHERE customers.CustomerID == @id"; var customers = new List<Customer2>(); using(var conn = new EntityConnection("name=AdventureWorksEntities")) { conn.Open(); using(var cmd = conn.CreateCommand()) { cmd.CommandText = esqlQuery; var param = new EntityParameter { ParameterName = "id", Value = id }; cmd.Parameters.Add(param); using(var reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { while (reader.Read()) { var customer = new Customer2 { CustomerID = int.Parse(reader["CustomerID"].ToString()), CustomerName = reader["FirstName"] + " " + reader["MiddleName"] + " " + reader["LastName"] }; customers.Add(customer); } } } conn.Close(); } return View(customers); }
private EntityParameter(EntityParameter source) : this() { DebugCheck.NotNull(source); source.CloneHelper(this); var cloneable = (_value as ICloneable); if (null != cloneable) { _value = cloneable.Clone(); } }
public void Executes_in_a_transaction_using_ExecutionStrategy_clears_parameters_for_retry() { var dbCommandMock = new Mock<DbCommand>(); dbCommandMock.Protected().Setup<Task<DbDataReader>>( "ExecuteDbDataReaderAsync", ItExpr.IsAny<CommandBehavior>(), ItExpr.IsAny<CancellationToken>()) .Throws(new InvalidOperationException("Foo")); var parameter = new EntityParameter(); var entityParameterCollection = new EntityParameterCollection(); dbCommandMock.Protected().SetupGet<DbParameterCollection>("DbParameterCollection").Returns(() => entityParameterCollection); var objectContext = Mock.Get(CreateObjectContext(dbCommandMock.Object)); var executionStrategyMock = new Mock<IDbExecutionStrategy>(); executionStrategyMock.Setup(m => m.RetriesOnFailure).Returns(true); executionStrategyMock.Setup( m => m.ExecuteAsync(It.IsAny<Func<Task<ObjectResult<object>>>>(), It.IsAny<CancellationToken>())) .Returns<Func<Task<ObjectResult<object>>>, CancellationToken>( (f, c) => { var result = f().Result; return Task.FromResult(result); }); MutableResolver.AddResolver<Func<IDbExecutionStrategy>>(key => (Func<IDbExecutionStrategy>)(() => executionStrategyMock.Object)); try { // To simulate the retry, we will execute the query twice. // and we validate that in both ocassions the same exception is received // if parameter were not cleared, the second call would fail with the // sql paramters contained in an another sql collection error. Assert.Equal( "Foo", Assert.Throws<InvalidOperationException>( () => ExceptionHelpers.UnwrapAggregateExceptions( () => objectContext.Object.ExecuteStoreQueryAsync<object>("Bar", parameter).GetAwaiter().GetResult())) .Message); Assert.Equal( "Foo", Assert.Throws<InvalidOperationException>( () => ExceptionHelpers.UnwrapAggregateExceptions( () => objectContext.Object.ExecuteStoreQueryAsync<object>("Bar", parameter).GetAwaiter().GetResult())) .Message); } finally { MutableResolver.ClearResolvers(); } }
/// <summary> /// Gets the location of the specified <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> in the collection. /// </summary> /// <returns> /// The zero-based location of the specified <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> that is a /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> /// in the collection. Returns -1 when the object does not exist in the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </returns> /// <param name="value"> /// The <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> to find. /// </param> public int IndexOf(EntityParameter value) { return IndexOf((object)value); }
/// <summary> /// Remove a EntityParameter with the given value from the collection /// </summary> /// <param name="value"> The parameter to remove </param> public void Remove(EntityParameter value) { Remove((object)value); }
/// <summary> /// Inserts a <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object into the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// at the specified index. /// </summary> /// <param name="index">The zero-based index at which value should be inserted.</param> /// <param name="value"> /// A <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object to be inserted in the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </param> public void Insert(int index, EntityParameter value) { Insert(index, (object)value); }
public void Executes_in_a_transaction_using_ExecutionStrategy_clears_parameters_for_retry() { var dbCommandMock = new Mock<DbCommand>(); dbCommandMock.Setup(m => m.ExecuteNonQuery()).Throws(new InvalidOperationException("Foo")); var objectContextMock = Mock.Get(CreateObjectContext(dbCommandMock.Object)); var executionStrategyMock = new Mock<IDbExecutionStrategy>(); var parameter = new EntityParameter(); var entityParameterCollection = new EntityParameterCollection(); dbCommandMock.Protected().SetupGet<DbParameterCollection>("DbParameterCollection").Returns(() => entityParameterCollection); objectContextMock.Setup( m => m.ExecuteInTransaction(It.IsAny<Func<int>>(), It.IsAny<IDbExecutionStrategy>(), It.IsAny<bool>(), It.IsAny<bool>())) .Returns<Func<int>, IDbExecutionStrategy, bool, bool>( (f, t, s, r) => { var result = f(); return result; }); executionStrategyMock.Setup(m => m.Execute(It.IsAny<Func<int>>())) .Returns<Func<int>>( f => { var result = f(); return result; }); MutableResolver.AddResolver<Func<IDbExecutionStrategy>>( key => (Func<IDbExecutionStrategy>)(() => executionStrategyMock.Object)); try { // To simulate the retry, we will execute the query twice. // and we validate that in both ocassions the same exception is received // if parameter were not cleared, the second call would fail with the // sql paramters contained in an another sql collection error. Assert.Equal( "Foo", Assert.Throws<InvalidOperationException>( () => objectContextMock.Object.ExecuteStoreCommand("foo", parameter)).Message); Assert.Equal( "Foo", Assert.Throws<InvalidOperationException>( () => objectContextMock.Object.ExecuteStoreCommand("foo", parameter)).Message); } finally { MutableResolver.ClearResolvers(); } }
/// <summary> /// Adds the specified <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object to the /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </summary> /// <returns> /// A new <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> object. /// </returns> /// <param name="value"> /// The <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> to add to the collection. /// </param> /// <exception cref="T:System.ArgumentException"> /// The <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" /> specified in the value parameter is already added to this or another /// <see /// cref="T:System.Data.Entity.Core.EntityClient.EntityParameterCollection" /> /// . /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The parameter passed was not a <see cref="T:System.Data.Entity.Core.EntityClient.EntityParameter" />. /// </exception> /// <exception cref="T:System.ArgumentNullException">The value parameter is null. </exception> public EntityParameter Add(EntityParameter value) { Add((object)value); return value; }
/// <summary> /// Finds the index in the collection of the given parameter object /// </summary> /// <param name="value"> The parameter to search for </param> /// <returns> The index of the parameter, -1 if not found </returns> public int IndexOf(EntityParameter value) { return(IndexOf((object)value)); }