// <summary> // Performs o-space loading for the type and returns false if the type is not in the model. // </summary> internal bool TryUpdateEntitySetMappingsForType(Type entityType) { Debug.Assert( entityType == ObjectContextTypeCache.GetObjectType(entityType), "Proxy type should have been converted to real type"); if (_entitySetMappingsCache.ContainsKey(entityType)) { return(true); } // We didn't find the type on first look, but this could be because the o-space loading // has not happened. So we try that, update our cached mappings, and try again. var typeToLoad = entityType; do { _workspace.LoadFromAssembly(typeToLoad.Assembly()); typeToLoad = typeToLoad.BaseType(); }while (typeToLoad != null && typeToLoad != typeof(Object)); lock (_entitySetMappingsUpdateLock) { if (_entitySetMappingsCache.ContainsKey(entityType)) { return(true); } UpdateEntitySetMappings(); } return(_entitySetMappingsCache.ContainsKey(entityType)); }
public TEntity Create(Type derivedEntityType) { if (!typeof(TEntity).IsAssignableFrom(derivedEntityType)) { throw Error.DbSet_BadTypeForCreate((object)derivedEntityType.Name, (object)typeof(TEntity).Name); } return((TEntity)this.InternalContext.CreateObject(ObjectContextTypeCache.GetObjectType(derivedEntityType))); }
public void ObjectContextTypeCache_GetObjectType_can_be_called_from_multiple_threads() { ExecuteInParallel( () => { var type = ObjectContextTypeCache.GetObjectType(typeof(FakeEntity)); Assert.Same(typeof(FakeEntity), type); }); }
/// <summary> /// Creates a new instance of an entity for the type of this set or for a type derived /// from the type of this set. /// Note that this instance is NOT added or attached to the set. /// The instance returned will be a proxy if the underlying context is configured to create /// proxies and the entity type meets the requirements for creating a proxy. /// </summary> /// <param name="derivedEntityType"> The type of entity to create. </param> /// <returns> The entity instance, which may be a proxy. </returns> public TEntity Create(Type derivedEntityType) { DebugCheck.NotNull(derivedEntityType); if (!typeof(TEntity).IsAssignableFrom(derivedEntityType)) { throw Error.DbSet_BadTypeForCreate(derivedEntityType.Name, typeof(TEntity).Name); } return((TEntity)InternalContext.CreateObject(ObjectContextTypeCache.GetObjectType(derivedEntityType))); }
public static void SetDisplayName( this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) { DebugCheck.NotNull(validationContext); var displayName = displayAttribute == null ? null : displayAttribute.GetName(); if (property == null) { var objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType); validationContext.DisplayName = displayName ?? objectType.Name; validationContext.MemberName = null; } else { validationContext.DisplayName = displayName ?? DbHelpers.GetPropertyPath(property); validationContext.MemberName = DbHelpers.GetPropertyPath(property); } }
public static void SetDisplayName( this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) { string str = displayAttribute == null ? (string)null : displayAttribute.GetName(); if (property == null) { Type objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType); validationContext.DisplayName = str ?? objectType.Name; validationContext.MemberName = (string)null; } else { validationContext.DisplayName = str ?? DbHelpers.GetPropertyPath(property); validationContext.MemberName = DbHelpers.GetPropertyPath(property); } }
// <summary> // Validates an entity or a complex type implementing IValidatableObject interface. // This method is virtual to allow mocking. // </summary> // <param name="entityValidationContext"> Validation context. Never null. </param> // <param name="property"> Property to validate. Null if this is the entity that will be validated. Never null if this is the complex type that will be validated. </param> // <returns> // Validation error as <see cref="IEnumerable{DbValidationError}" /> . Empty if no errors. Never null. // </returns> // <remarks> // Note that <paramref name="property" /> is used to figure out what needs to be validated. If it not null the complex // type will be validated otherwise the entity will be validated. // Also if this is an IValidatableObject complex type but the instance (.CurrentValue) is null we won't validate // anything and will not return any errors. The reason for this is that Validation is supposed to validate using // information the user provided and not some additional implicit rules. (ObjectContext will throw for operations // that involve null complex properties). // </remarks> public virtual IEnumerable <DbValidationError> Validate( EntityValidationContext entityValidationContext, InternalMemberEntry property) { DebugCheck.NotNull(entityValidationContext); Debug.Assert( (property == null && entityValidationContext.InternalEntity.Entity is IValidatableObject) || (property != null && (property.CurrentValue == null || property.CurrentValue is IValidatableObject)), "Neither entity nor complex type implements IValidatableObject."); if (property != null && property.CurrentValue == null) { return(Enumerable.Empty <DbValidationError>()); } var validationContext = entityValidationContext.ExternalValidationContext; validationContext.SetDisplayName(property, _displayAttribute); var validatableObject = (IValidatableObject)(property == null ? entityValidationContext.InternalEntity.Entity : property.CurrentValue); IEnumerable <ValidationResult> validationResults = null; try { validationResults = validatableObject.Validate(validationContext); } catch (Exception ex) { throw new DbUnexpectedValidationException( Strings.DbUnexpectedValidationException_IValidatableObject( validationContext.DisplayName, ObjectContextTypeCache.GetObjectType(validatableObject.GetType())), ex); } return(DbHelpers.SplitValidationResults( validationContext.MemberName, validationResults ?? Enumerable.Empty <ValidationResult>())); }
internal virtual void Configure(EdmProperty property) { DebugCheck.NotNull(property); Debug.Assert(property.TypeUsage != null); var clone = Clone(); var mergedConfiguration = clone.MergeWithExistingConfiguration( property, errorMessage => { var propertyInfo = property.GetClrPropertyInfo(); var declaringTypeName = propertyInfo == null ? string.Empty : ObjectContextTypeCache.GetObjectType(propertyInfo.DeclaringType). FullNameWithNesting(); return(Error.ConflictingPropertyConfiguration(property.Name, declaringTypeName, errorMessage)); }, inCSpace: true, fillFromExistingConfiguration: false); mergedConfiguration.ConfigureProperty(property); }
internal virtual void Configure(EdmProperty property) { DebugCheck.NotNull(property); Debug.Assert(property.TypeUsage != null); var existingConfiguration = property.GetConfiguration() as PrimitivePropertyConfiguration; if (existingConfiguration != null) { string errorMessage; if ((existingConfiguration.OverridableConfigurationParts & OverridableConfigurationParts.OverridableInCSpace) != OverridableConfigurationParts.OverridableInCSpace && !existingConfiguration.IsCompatible(this, inCSpace: true, errorMessage: out errorMessage)) { var propertyInfo = property.GetClrPropertyInfo(); var declaringTypeName = propertyInfo == null ? string.Empty : ObjectContextTypeCache.GetObjectType(propertyInfo.DeclaringType). FullNameWithNesting(); throw Error.ConflictingPropertyConfiguration(property.Name, declaringTypeName, errorMessage); } // Choose the more derived type for the merged configuration PrimitivePropertyConfiguration mergedConfiguration; if (existingConfiguration.GetType().IsAssignableFrom(GetType())) { mergedConfiguration = (PrimitivePropertyConfiguration)MemberwiseClone(); } else { mergedConfiguration = (PrimitivePropertyConfiguration)existingConfiguration.MemberwiseClone(); mergedConfiguration.CopyFrom(this); } mergedConfiguration.FillFrom(existingConfiguration, inCSpace: true); property.SetConfiguration(mergedConfiguration); } else { property.SetConfiguration(this); } if (IsNullable != null) { property.Nullable = IsNullable.Value; } if (ConcurrencyMode != null) { property.ConcurrencyMode = ConcurrencyMode.Value; } if (DatabaseGeneratedOption != null) { property.SetStoreGeneratedPattern((StoreGeneratedPattern)DatabaseGeneratedOption.Value); if (DatabaseGeneratedOption.Value == ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity) { property.Nullable = false; } } }
public virtual IEnumerable <DbValidationError> Validate( EntityValidationContext entityValidationContext, InternalMemberEntry property) { if (property != null && property.CurrentValue == null) { return(Enumerable.Empty <DbValidationError>()); } ValidationContext validationContext = entityValidationContext.ExternalValidationContext; validationContext.SetDisplayName(property, this._displayAttribute); IValidatableObject validatableObject = property == null ? (IValidatableObject)entityValidationContext.InternalEntity.Entity : (IValidatableObject)property.CurrentValue; IEnumerable <ValidationResult> validationResults; try { validationResults = validatableObject.Validate(validationContext); } catch (Exception ex) { throw new DbUnexpectedValidationException(Strings.DbUnexpectedValidationException_IValidatableObject((object)validationContext.DisplayName, (object)ObjectContextTypeCache.GetObjectType(validatableObject.GetType())), ex); } return(DbHelpers.SplitValidationResults(validationContext.MemberName, validationResults ?? Enumerable.Empty <ValidationResult>())); }
internal virtual void Configure(EdmProperty property) { this.Clone().MergeWithExistingConfiguration(property, (Func <string, Exception>)(errorMessage => { PropertyInfo clrPropertyInfo = property.GetClrPropertyInfo(); return(Error.ConflictingPropertyConfiguration((object)property.Name, clrPropertyInfo == (PropertyInfo)null ? (object)string.Empty : (object)ObjectContextTypeCache.GetObjectType(clrPropertyInfo.DeclaringType).FullNameWithNesting(), (object)errorMessage)); }), true, false).ConfigureProperty(property); }