public KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpression specifiedKey) { List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } return(ResourceInstanceUtil.CreateKeyedResourceInstance(specifiedKey, container, properties.ToArray())); }
internal KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys, bool populateNavProps) { KeyExpressions relatedForeignKeys = new KeyExpressions(); ResourceInstanceKey key = ResourceInstanceUtil.CreateUniqueKey(container, this, relatedForeignKeys, existingKeys); List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); // populate the non-key, non-navigation properties // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { if (p.Facets.IsIdentity) { continue; } ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } if (populateNavProps) { // populate the navigation properties, but don't go by key, as some foreign keys MAY NOT HAVE ASSOCIATED NAVIGATION PROPERTIES // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation)) { // find a key for this navigation property KeyExpression navPropKey = null; foreach (KeyExpression keyExp in relatedForeignKeys) { //if (p.Type.Equals(keyExp.ResourceType) // || p.Type is ResourceType && (p.Type as ResourceType).BaseTypes.Contains(keyExp.ResourceType)) if (p.OtherAssociationEnd.ResourceType.Equals(keyExp.ResourceType)) { navPropKey = keyExp; break; } } ResourceContainer associatedContainer = container.FindDefaultRelatedContainer(p); if (navPropKey == null) { if (p.OtherAssociationEnd.ResourceType.Key.Properties.OfType <ResourceProperty>() .Where(rp => rp.ForeignKeys.Any()) .Any(rp => rp.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType.Equals(this))) { // this association has a fk back to the current type, so it cannot be based on any existing entity AstoriaTestLog.WriteLineIgnore("Skipping nav prop '{0}.{1}' due to foreign key constraint on entity being created", this.Name, p.Name); continue; } else { navPropKey = associatedContainer.Workspace.GetRandomExistingKey(associatedContainer, p.OtherAssociationEnd.ResourceType); } } if (navPropKey != null) { if (p.OtherAssociationEnd.Multiplicity == Multiplicity.Many) { properties.Add(ResourceInstanceUtil.CreateCollectionInstanceProperty(new KeyExpressions(navPropKey), navPropKey.ResourceContainer, p)); } else { properties.Add(ResourceInstanceUtil.CreateRefInstanceProperty(navPropKey, navPropKey.ResourceContainer, p)); } } } } return(ResourceInstanceUtil.CreateKeyedResourceInstance(key.CreateKeyExpression(container, this), container, properties.ToArray())); }