public void Remove(object entityObject) { CustomUtils.CheckArgumentNotNull(entityObject, "entityObject"); CustomEntityType entityType = CheckTypeCompatibility(entityObject); RemoveInternal(entityObject); }
public void Update(object entityObject) { CustomUtils.CheckArgumentNotNull(entityObject, "entityObject"); CustomEntityType entityType = CheckTypeCompatibility(entityObject); UpdateInternal(entityObject); RunPostUpdateStoreTriggers(entityObject, entityType); }
protected void RunPostUpdateStoreTriggers(object entityObject, CustomEntityType entityType) { AssignStoreGeneratedValues(entityObject, entityType); if (!_parentContainer.DisableStoreValidation) { ValidatePrimitiveConstraints(entityObject, entityType); } }
public static IEdmModel EdmTypeKindInterfaceCriticalKindValueUnexpectedOnlyModel() { var model = new EdmModel(); var customEntity = new CustomEntityType((EdmTypeKind)123); model.AddElement(customEntity); return(model); }
public static IEdmModel InterfaceCriticalEnumerableMustNotHaveNullElementsOnlyModel() { var model = new EdmModel(); var customEntity = new CustomEntityType(new List <IEdmProperty>() { null }); model.AddElement(customEntity); return(model); }
private CustomEntityType CheckTypeCompatibility(object entityObject) { Debug.Assert(null != entityObject); CustomEntityType entityType = MetadataWorkspace.GetEntityType(entityObject.GetType()); if (!EntitySetType.BaseElementType.IsAssignableFrom(entityType)) { throw new InvalidOperationException(String.Format( "The base element type '{0}' of the entity set '{1}' is not compatible with " + "the entity type '{2}' of the object '{3}'.", EntitySetType.BaseElementType, EntitySetType.Name, entityType, entityObject )); } return(entityType); }
public static IEdmModel AllInterfaceCriticalModel() { var model = new EdmModel(); var valueTerm = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetString(true)); model.AddElement(valueTerm); var badString = new CustomStringConstant("foo", EdmExpressionKind.None, EdmValueKind.Integer); var valueAnnotation = new EdmAnnotation( valueTerm, valueTerm, badString); model.AddVocabularyAnnotation(valueAnnotation); var mutableValueAnnotationueAnnotation = new MutableValueAnnotation() { Target = valueTerm }; model.AddVocabularyAnnotation(mutableValueAnnotationueAnnotation); var customEntity = new CustomEntityType(new List <IEdmProperty>() { null }); model.AddElement(customEntity); var entity = new EdmEntityType("DefaultNamespace", "bar"); var entity2 = new EdmEntityType("DefaultNamespace", "bar2"); var navProperty = new StubEdmNavigationProperty("Nav") { DeclaringType = entity, Type = new EdmEntityTypeReference(entity2, false) }; navProperty.Partner = navProperty; entity.AddProperty(navProperty); model.AddElement(entity); model.AddElement(entity2); return(model); }
public IQueryable <EntityObjectType> CreateQueryOfT <EntityObjectType>(string entitySetName) { CustomUtils.CheckArgumentNotNull(entitySetName, "entitySetName"); CustomEntitySetType entitySetType = _metadata.GetEntitySet(entitySetName); CustomEntityType entityType = _metadata.GetEntityType(typeof(EntityObjectType)); if (!entitySetType.BaseElementType.IsAssignableFrom(entityType)) { throw new InvalidOperationException(String.Format( "The base element type '{0}' of the entity set '{1}' is not compatible with " + "the queried entity type '{2}'.", entitySetType.BaseElementType, entitySetName, entityType )); } CustomEntitySet entitySet = GetEntitySetByName(entitySetName); Debug.Assert(null != entitySet); Debug.Assert(typeof(IEnumerable <EntityObjectType>).IsAssignableFrom(entitySet.GetType())); return(((IEnumerable <EntityObjectType>)entitySet).AsQueryable()); }
protected override void ValidateStateTransitionInternal( CustomObjectContext objectContext, RelatedEndEntityObject relatedEntityObject, CustomStoreOperationKind changeKind ) { if (Object.ReferenceEquals(_thisEndObject, relatedEntityObject)) { throw new InvalidOperationException(String.Format( "Cannot add entity object '{0}' to its one one-to-many collection.", _thisEndObject )); } if (CustomStoreOperationKind.Add == changeKind) { if (null != RelationshipMetadata.To.RelatedProperty) { var sameOneEndRoleObject = RelationshipMetadata.To.RelatedProperty.GetValue(relatedEntityObject); if ( !Object.ReferenceEquals(sameOneEndRoleObject, null) && !Object.ReferenceEquals(sameOneEndRoleObject, _thisEndObject)) { #if _DO_NOT_REMOVE_SILENTLY_SAME_ROLE_INSTANCE throw new InvalidOperationException(String.Format( "Entity object '{0}' is already associated with another " + "instance '{1}' of principal end in the one-to-many relationship " + "between '{2}' and '{3}'.", relatedEntityObject, sameOneEndRoleObject, typeof(ThisEndEntityObject), typeof(RelatedEndEntityObject) )); #else CustomEntityType entityType = RelationshipMetadata.From.EntityType; // TODO: do we need this assert? saw it failing in Astoria tests; may be it is // caused by multiple object contexts loaded from the same data source? // in this implementation all object contexts share same objects #if _NOT_ASTORIA_TESTS Debug.Assert(entityType.ClrObjectType.IsAssignableFrom(sameOneEndRoleObject.GetType())); #endif object otherCollection = RelationshipMetadata.GetValue(sameOneEndRoleObject); Debug.Assert(null != otherCollection); if (otherCollection is CustomObjectCollectionBase) { bool removed = ((CustomObjectCollectionBase)otherCollection).Remove(relatedEntityObject); // TODO: need to investigate; saw a failure here with Northwind model in Astoria tests // same problem with many object contexts loaded from the same data source as above? #if _NOT_ASTORIA_TESTS Debug.Assert(removed); #endif } else { MethodInfo removeMethod = otherCollection.GetType().GetMethod("Remove"); Debug.Assert(null != removeMethod); removeMethod.Invoke(otherCollection, new object[] { relatedEntityObject }); } #endif } } } }
public static IEdmModel EdmTypeKindInterfaceCriticalKindValueUnexpectedOnlyModel() { var model = new EdmModel(); var customEntity = new CustomEntityType((EdmTypeKind)123); model.AddElement(customEntity); return model; }
public static IEdmModel AllInterfaceCriticalModel() { var model = new EdmModel(); var valueTerm = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetString(true)); model.AddElement(valueTerm); var badString = new CustomStringConstant("foo", EdmExpressionKind.None, EdmValueKind.Integer); var valueAnnotation = new EdmAnnotation( valueTerm, valueTerm, badString); model.AddVocabularyAnnotation(valueAnnotation); var mutableValueAnnotationueAnnotation = new MutableValueAnnotation() { Target = valueTerm }; model.AddVocabularyAnnotation(mutableValueAnnotationueAnnotation); var customEntity = new CustomEntityType(new List<IEdmProperty>() { null }); model.AddElement(customEntity); var entity = new EdmEntityType("DefaultNamespace", "bar"); var entity2 = new EdmEntityType("DefaultNamespace", "bar2"); var navProperty = new StubEdmNavigationProperty("Nav") { DeclaringType = entity, Type = new EdmEntityTypeReference(entity2, false) }; navProperty.Partner = navProperty; entity.AddProperty(navProperty); model.AddElement(entity); model.AddElement(entity2); return model; }
public static IEdmModel InterfaceCriticalEnumerableMustNotHaveNullElementsOnlyModel() { var model = new EdmModel(); var customEntity = new CustomEntityType(new List<IEdmProperty>() { null }); model.AddElement(customEntity); return model; }
private void ValidatePrimitiveConstraints(object entityObject, CustomEntityType entityType) { foreach (var member in entityType.Members) { object currValue = member.GetValue(entityObject); if (null == currValue) { if (member.IsNullable) { continue; } else { throw new CustomStoreConstraintViolationException(String.Format( "Property '{0}' of the object '{1}' cannot be null.", member, entityObject )); } } if (member is CustomPropertyType) { var property = (CustomPropertyType)member; Debug.Assert(!(property.IsPrimaryKey && property.IsNullable)); if (property.IsPrimaryKey && null == property.ForeignKeyConstraint) { if (CustomEntityKey.IsDefaultKeyValue(property, currValue)) { throw new CustomStoreConstraintViolationException(String.Format( "Primary key property '{0}.{1}' of the object '{2}' " + "cannot be set to its default value {3} when saved. " + "Only primary key properties being foreign keys are " + "allowed to have its default values. Did you forget to" + "annotate the property with '{4}'?", entityType, member, entityObject, currValue, typeof(Metadata.CustomForeignKeyConstraintAttribute).Name )); } } if (property.MaximumLength.HasValue) { int actualLength; bool lengthFound = TryGetLength(currValue, out actualLength); Debug.Assert(lengthFound); if (actualLength > property.MaximumLength.Value) { throw new CustomStoreConstraintViolationException(String.Format( "Length of the property '{0}.{1}' of the object '{2}' " + "must be less or equal to {3}. The actual value is {4}.", entityType, member, entityObject, property.MaximumLength.Value, actualLength )); } } } } }
private void AssignStoreGeneratedValues(object entityObject, CustomEntityType entityType) { foreach (var property in entityType.Propeties.Where(p => p.IsStoreGenerated == true)) { if (property.IsReadOnly || null != property.ForeignKeyConstraint) { continue; } if (property.IsPrimaryKey) { object currKeyValue = property.GetValue(entityObject); if (!CustomEntityKey.IsDefaultKeyValue(property, currKeyValue)) { continue; } } Debug.Assert(null == property.ForeignKeyConstraint); object generatedValue = null; if (property.ClrType.IsPrimitive) { generatedValue = NextPrimitiveSequenceValue(property.ClrType); } else if (property.ClrType == typeof(Guid)) { generatedValue = Guid.NewGuid(); } else if (property.ClrType == typeof(DateTime)) { generatedValue = NextDateTimeSequenceValue(); } else if (property.ClrType == typeof(String)) { string strValue = Guid.NewGuid().ToString(); if (property.MaximumLength.HasValue && strValue.Length > property.MaximumLength) { strValue = strValue.Substring(0, property.MaximumLength.Value); } generatedValue = strValue; } else if ( property.ClrType == typeof(Decimal) || property.ClrType == typeof(Single) || property.ClrType == typeof(Double) ) { generatedValue = Convert.ChangeType( NextPrimitiveSequenceValue(typeof(Int32)), property.ClrType ); } else { Debug.Fail("Unsupported key type", property.ClrType.Name); } property.SetValue(entityObject, generatedValue); } }