protected void InsertReferenceNavProperty() { ForEachResourceType( (resourceType, resourceContainer, workspace) => { CreateContext(resourceType, workspace); #if !ClientSKUFramework object entity = ExecuteURI(resourceType, String.Format("/{0}", resourceContainer.Name)); Uri entityUri = null; IEdmEntityType entityType = DataServiceMetadata.GetEntityType(entity); if (entityType == null) { return; } CurrentContext.TryGetUri(entity, out entityUri); foreach (string refNavProperty in entityType.ReferenceNavigationProperties()) { ResourceType navResType = workspace.ServiceContainer.ResourceTypes.FirstOrDefault(rt => rt.Name == refNavProperty); if (navResType != null) { object navEntity = navResType.CreateInstance(false); string navPropertyURI = String.Format("{0}/{1}", entityUri.Segments[entityUri.Segments.Length - 1], refNavProperty); CurrentContext.AddObject(navPropertyURI, navEntity); CurrentContext.EnsureInsert(navEntity, navResType); } } ExecuteAndValidate(() => CurrentContext.SaveChanges(SaveChangeOption)); if (ContinueTest) { if (ChainedFunction != null) { ChainedFunction(); } } #endif }, true); }
protected void EnsureInsert(DataServiceContext context, object entity, string entitySetName, Workspace workspace, string skipEntitySet) { #if !ClientSKUFramework IEdmEntityType entityType = null; if (DataServiceMetadata.ServiceMetadata == null) { DataServiceMetadata.LoadServiceMetadata(workspace.ServiceUri); } if (DataServiceMetadata.EntityTypes.Any(eType => eType.Name == entitySetName)) { entityType = DataServiceMetadata.EntityTypes.First(eType => eType.Name == entitySetName); } if (entityType == null && DataServiceMetadata.EntityTypes.Any(eType => eType.Name == entity.GetType().Name)) { entityType = DataServiceMetadata.EntityTypes.First(eType => eType.Name == entity.GetType().Name); entitySetName = entity.GetType().Name; } if (entityType == null) { return; } foreach (IEdmNavigationProperty navProperty in entityType.NavigationProperties()) { if (context.Links.All(ld => (ld.SourceProperty != navProperty.Name))) { if (navProperty.TargetMultiplicity() == EdmMultiplicity.One && navProperty.Name != skipEntitySet) { IEdmEntityType navProperyEntityType = DataServiceMetadata.GetEntityType(navProperty); ResourceType resourceType = workspace.ServiceContainer.ResourceTypes.First(rt => rt.Name == navProperyEntityType.Name); object instance = resourceType.CreateInstance(false); context.AddObject(navProperyEntityType.Name == "College" ? "Colleges" : navProperyEntityType.Name, instance); context.SetLink(entity, navProperty.Name, instance); context.SaveChangesDefaultOptions = SaveChangesOptions.BatchWithSingleChangeset; } } } #endif }
protected object InsertAction(ResourceType resourceType, ResourceContainer resourceContainer, Workspace workspace) { AstoriaTestLog.WriteLine("Inserting into entity set :{0} , entity type :{1}", resourceContainer.Name, resourceType.Name); object entity = null; if (resourceType.Name == "AllTypesComplexEntity") { IQueryable results = CurrentContext.ExecuteOfT(resourceType.ClientClrType, new Uri(String.Format("{0}?$top=1", resourceContainer.Name), UriKind.RelativeOrAbsolute)).AsQueryable(); IEnumerator enumerat = results.GetEnumerator(); enumerat.MoveNext(); entity = enumerat.Current; ResourceProperty keyProperty = resourceType.Properties.Cast <ResourceProperty>().First(prop => prop.PrimaryKey != null); entity.GetType().GetProperty(keyProperty.Name).SetValue(entity, null, null); } else { entity = resourceType.CreateInstance(false); } CurrentContext.AddObject(resourceContainer.Name, entity); CurrentContext.EnsureInsert(entity, resourceType); EnsureInsert(CurrentContext.UnderlyingContext, entity, resourceContainer.Name, workspace, ""); ExecuteAndValidate(() => { CurrentContext.SaveChanges(SaveChangeOption); }); return(entity); }