public void PropertyValuesEqual_checks_value_equality_for_value_types_and_reference_equality_for_reference_types() { Equality_tests(DbHelpers.PropertyValuesEqual); Assert.False(DbHelpers.PropertyValuesEqual(new ExecutionStrategyKey("foo", "bar"), new ExecutionStrategyKey("foo", "bar"))); }
// <summary> // Creates the underlying <see cref="DbConnection" /> (which may actually be an <see cref="EntityConnection" />) // if it does not already exist. // </summary> private void Initialize() { if (UnderlyingConnection == null) { Debug.Assert(AppConfig != null); string name; if (_connectionInfo != null) { var connection = _connectionInfo.GetConnectionString(AppConfig); InitializeFromConnectionStringSetting(connection); _connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo; _connectionStringName = connection.Name; } // If the name or connection string is a simple name or is in the form "name=xyz" then use // that name to try to load from the app/web config file. else if (!DbHelpers.TryGetConnectionName(_nameOrConnectionString, out name) || !TryInitializeFromAppConfig(name, AppConfig)) { // If the connection string is of the form name=, but the name was not found in the config file // then always throw since we always interpret name= to mean find in the config file only. if (name != null && DbHelpers.TreatAsConnectionString(_nameOrConnectionString)) { throw Error.DbContext_ConnectionStringNotFound(name); } // If the name or connection string is a full EF connection string, then create an EntityConnection from it. if (DbHelpers.IsFullEFConnectionString(_nameOrConnectionString)) { UnderlyingConnection = new EntityConnection(_nameOrConnectionString); } else { if (base.ProviderName != null) { CreateConnectionFromProviderName(base.ProviderName); } else { // Otherwise figure out the connection factory to use (either the default, // the one set in code, or one provided by DbContextInfo via the AppSettings property UnderlyingConnection = DbConfiguration.DependencyResolver.GetService <IDbConnectionFactory>() .CreateConnection(name ?? _nameOrConnectionString); if (UnderlyingConnection == null) { throw Error.DbContext_ConnectionFactoryReturnedNullConnection(); } } } if (name != null) { _connectionStringOrigin = DbConnectionStringOrigin.Convention; _connectionStringName = name; } else { _connectionStringOrigin = DbConnectionStringOrigin.UserCode; } } OnConnectionInitialized(); } Debug.Assert(UnderlyingConnection != null, "Connection should have been initialized by some mechanism."); }
public void KeyValuesEqual_checks_value_equality_for_value_types_and_Equals_implementation_for_reference_types() { Equality_tests(DbHelpers.KeyValuesEqual); Assert.True(DbHelpers.KeyValuesEqual(new ExecutionStrategyKey("foo", "bar"), new ExecutionStrategyKey("foo", "bar"))); }
// <summary> // Validates that the given name is a property of the declaring type (either on the CLR type or in the EDM) // and that it is a complex or scalar property rather than a nav property and then returns metadata about // the property. // </summary> // <param name="internalContext"> The internal context. </param> // <param name="declaringType"> The type that the property is declared on. </param> // <param name="requestedType"> The type of property requested, which may be 'object' if any type can be accepted. </param> // <param name="propertyName"> Name of the property. </param> // <returns> Metadata about the property, or null if the property does not exist or is a navigation property. </returns> public static PropertyEntryMetadata ValidateNameAndGetMetadata( InternalContext internalContext, Type declaringType, Type requestedType, string propertyName) { DebugCheck.NotNull(internalContext); DebugCheck.NotNull(declaringType); DebugCheck.NotNull(requestedType); DebugCheck.NotEmpty(propertyName); Type propertyType; DbHelpers.GetPropertyTypes(declaringType).TryGetValue(propertyName, out propertyType); var metadataWorkspace = internalContext.ObjectContext.MetadataWorkspace; var edmType = metadataWorkspace.GetItem <StructuralType>(declaringType.FullNameWithNesting(), DataSpace.OSpace); var isMapped = false; var isComplex = false; EdmMember member; edmType.Members.TryGetValue(propertyName, false, out member); if (member != null) { // If the property is in the model, then it must be a scalar or complex property, not a nav prop var edmProperty = member as EdmProperty; if (edmProperty == null) { return(null); } if (propertyType == null) { var asPrimitive = edmProperty.TypeUsage.EdmType as PrimitiveType; if (asPrimitive != null) { propertyType = asPrimitive.ClrEquivalentType; } else { Debug.Assert( edmProperty.TypeUsage.EdmType is StructuralType, "Expected a structural type if property type is not primitive."); var objectItemCollection = (ObjectItemCollection)metadataWorkspace.GetItemCollection(DataSpace.OSpace); propertyType = objectItemCollection.GetClrType((StructuralType)edmProperty.TypeUsage.EdmType); } } isMapped = true; isComplex = edmProperty.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType; } else { // If the prop is not in the model, then it must have a getter or a setter var propertyGetters = DbHelpers.GetPropertyGetters(declaringType); var propertySetters = DbHelpers.GetPropertySetters(declaringType); if (!(propertyGetters.ContainsKey(propertyName) || propertySetters.ContainsKey(propertyName))) { return(null); } Debug.Assert(propertyType != null, "If the property has a getter or setter, then it must exist and have a type."); } if (!requestedType.IsAssignableFrom(propertyType)) { throw Error.DbEntityEntry_WrongGenericForProp( propertyName, declaringType.Name, requestedType.Name, propertyType.Name); } return(new PropertyEntryMetadata(declaringType, propertyType, propertyName, isMapped, isComplex)); }
public static string GetPropertyPath(InternalMemberEntry property) { return(string.Join(".", DbHelpers.GetPropertyPathSegments(property).Reverse <string>())); }
public static bool TryParsePath(Expression expression, out string path) { path = (string)null; Expression expression1 = expression.RemoveConvert(); MemberExpression memberExpression = expression1 as MemberExpression; MethodCallExpression methodCallExpression = expression1 as MethodCallExpression; if (memberExpression != null) { string name = memberExpression.Member.Name; string path1; if (!DbHelpers.TryParsePath(memberExpression.Expression, out path1)) { return(false); } path = path1 == null ? name : path1 + "." + name; } else if (methodCallExpression != null) { string path1; if (methodCallExpression.Method.Name == "Select" && methodCallExpression.Arguments.Count == 2 && (DbHelpers.TryParsePath(methodCallExpression.Arguments[0], out path1) && path1 != null)) { LambdaExpression lambdaExpression = methodCallExpression.Arguments[1] as LambdaExpression; string path2; if (lambdaExpression != null && DbHelpers.TryParsePath(lambdaExpression.Body, out path2) && path2 != null) { path = path1 + "." + path2; return(true); } } return(false); } return(true); }
private ObjectQuery <DbDataRecord> GetDatabaseValuesQuery() { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("SELECT "); this.AppendEntitySqlRow(queryBuilder, "X", this.OriginalValues); string str1 = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)DbHelpers.QuoteIdentifier(this._stateEntry.EntitySet.EntityContainer.Name), (object)DbHelpers.QuoteIdentifier(this._stateEntry.EntitySet.Name)); string str2 = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)DbHelpers.QuoteIdentifier(this.EntityType.NestingNamespace()), (object)DbHelpers.QuoteIdentifier(this.EntityType.Name)); queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, " FROM (SELECT VALUE TREAT (Y AS {0}) FROM {1} AS Y) AS X WHERE ", (object)str2, (object)str1); EntityKeyMember[] entityKeyValues = this._stateEntry.EntityKey.EntityKeyValues; ObjectParameter[] objectParameterArray = new ObjectParameter[entityKeyValues.Length]; for (int index = 0; index < entityKeyValues.Length; ++index) { if (index > 0) { queryBuilder.Append(" AND "); } string name = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "p{0}", (object)index.ToString((IFormatProvider)CultureInfo.InvariantCulture)); queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, "X.{0} = @{1}", (object)DbHelpers.QuoteIdentifier(entityKeyValues[index].Key), (object)name); objectParameterArray[index] = new ObjectParameter(name, entityKeyValues[index].Value); } return(this._internalContext.ObjectContext.CreateQuery <DbDataRecord>(queryBuilder.ToString(), objectParameterArray)); }