internal void GenerateAssociationSets( SimpleMappingContext mappingContext, UniqueIdentifierService uniqueEntityContainerNames, UniqueIdentifierService globallyUniqueTypeNames) { IEnumerable <AssociationSet> associationSetsFromNonCollapsibleItems; var collapsibleItems = CollapsibleEntityAssociationSets.CreateCollapsibleItems( mappingContext.StoreModel.Containers.Single().BaseEntitySets, out associationSetsFromNonCollapsibleItems); foreach (var set in associationSetsFromNonCollapsibleItems) { GenerateAssociationSet( mappingContext, set, uniqueEntityContainerNames, globallyUniqueTypeNames); } foreach (var item in collapsibleItems) { GenerateAssociationSet( mappingContext, item, uniqueEntityContainerNames, globallyUniqueTypeNames); } }
private static AssociationSetMapping BuildAssociationSetMapping( CollapsibleEntityAssociationSets collapsibleItem, EntityContainerMapping entityContainerMapping, SimpleMappingContext mappingContext) { Debug.Assert(collapsibleItem != null, "collapsibleItem != null"); Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null"); Debug.Assert(mappingContext != null, "mappingContext != null"); var modelAssociationSet = mappingContext[collapsibleItem]; if (modelAssociationSet.ElementType.IsForeignKey) { return(null); } var associationSetMapping = new AssociationSetMapping( modelAssociationSet, collapsibleItem.EntitySet, entityContainerMapping); var count = collapsibleItem.AssociationSets.Count; if (count > 0) { associationSetMapping.SourceEndMapping = BuildEndPropertyMapping( collapsibleItem.GetStoreAssociationSetEnd(0).AssociationSetEnd, mappingContext); } if (count > 1) { associationSetMapping.TargetEndMapping = BuildEndPropertyMapping( collapsibleItem.GetStoreAssociationSetEnd(1).AssociationSetEnd, mappingContext); } return(associationSetMapping); }
public void Can_add_and_get_mapping_for_collapsed_entity_sets() { var storeEntity = CreateEntityType("storeEntity"); var storeEntitySet = EntitySet.Create("storeEntitySet", null, null, null, storeEntity, null); var collapsibleAssociationSet = new CollapsibleEntityAssociationSets(storeEntitySet); var associationType = AssociationType.Create("modelAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null); var associationSet = AssociationSet.Create("modelAssociationType", associationType, null, null, null); var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true); mappingContext.AddMapping(collapsibleAssociationSet, associationSet); Assert.Same(associationSet, mappingContext[collapsibleAssociationSet]); }
public void ConceptualAssociationSets_returns_associationsets_for_collapsed_entity_sets() { var storeEntity = CreateEntityType("storeEntity"); var storeEntitySet = EntitySet.Create("storeEntitySet", null, null, null, storeEntity, null); var collapsibleAssociationSet = new CollapsibleEntityAssociationSets(storeEntitySet); var associationType = AssociationType.Create("modelAssociationType", "ns", false, DataSpace.CSpace, null, null, null, null); var associationSet = AssociationSet.Create("modelAssociationType", associationType, null, null, null); var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true); Assert.Empty(mappingContext.ConceptualAssociationSets()); mappingContext.AddMapping(collapsibleAssociationSet, associationSet); Assert.Equal(1, mappingContext.ConceptualAssociationSets().Count()); Assert.Same(associationSet, mappingContext.ConceptualAssociationSets().Single()); }
public void BuildAssociationSetMappings_builds_conceptual_association_set_mapping_for_collapsed_store_entity_sets() { #region Setting up many to many relationship in the SSpace Teacher * -- 1 TeacherStudents 1 -- * Teachers var joinStoreEntityType = EntityType.Create( "TeacherStudents", "ns.Store", DataSpace.SSpace, new[] { "JoinTeacherId", "JoinStudentId" }, new[] { CreateStoreProperty("JoinTeacherId", "int"), CreateStoreProperty("JoinStudentId", "int") }, null); var joinStoreEntitySet = EntitySet.Create("TeacherStudentsSet", "dbo", "TeacherStudentTable", null, joinStoreEntityType, null); var storeTeacherEntityType = EntityType.Create( "Teacher", "ns.Store", DataSpace.SSpace, new[] { "TeacherId" }, new[] { CreateStoreProperty("TeacherId", "int") }, null); var storeTeacherEntitySet = EntitySet.Create("TeachersSet", "dbo", "Teachers", null, storeTeacherEntityType, null); var storeStudentEntityType = EntityType.Create( "Student", "ns.Store", DataSpace.SSpace, new[] { "StudentId" }, new[] { CreateStoreProperty("StudentId", "int") }, null); var storeStudentEntitySet = EntitySet.Create("StudentSet", "dbo", "Students", null, storeStudentEntityType, null); var storeTeachersEndMember = AssociationEndMember.Create( "Teachers", storeTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var storeTeacherStudentsfromTeachersEndMember = AssociationEndMember.Create( "TeacherStudents_fromTeachers", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null); var storeTeacherAssociationType = AssociationType.Create( "Teacher_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace, storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, new ReferentialConstraint( storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, storeTeacherEntityType.KeyProperties, joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinTeacherId")), null); var storeTeacherAssociationSet = AssociationSet.Create( "Teacher_TeacherStudents", storeTeacherAssociationType, storeTeacherEntitySet, joinStoreEntitySet, null); var storeStudentsEndMember = AssociationEndMember.Create( "Students", storeStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var storeTeacherStudentsfromStudentsEndMember = AssociationEndMember.Create( "TeacherStudents_fromStudents", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null); var storeStudentAssociationType = AssociationType.Create( "Student_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace, storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, new ReferentialConstraint( storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, storeStudentEntityType.KeyProperties, joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinStudentId")), null); var storeStudentAssociationSet = AssociationSet.Create( "Student_TeacherStudents", storeStudentAssociationType, storeStudentEntitySet, joinStoreEntitySet, null); var collapsedAssociationSet = new CollapsibleEntityAssociationSets(joinStoreEntitySet); collapsedAssociationSet.AssociationSets.Add(storeTeacherAssociationSet); collapsedAssociationSet.AssociationSets.Add(storeStudentAssociationSet); #endregion #region Setting up many to many relationship in the CSpace Teacher * -- * Teachers var conceptualContainer = EntityContainer.Create("ConceptualContainer", DataSpace.CSpace, null, null, null); var edmIntTypeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); var conceptualTeacherEntityType = EntityType.Create( "Teacher", "ns", DataSpace.CSpace, new[] { "TeacherId" }, new[] { EdmProperty.Create("TeacherId", edmIntTypeUsage) }, null); var conceptualTeacherEntitySet = EntitySet.Create("TeachersSet", null, null, null, conceptualTeacherEntityType, null); var conceptualStudentEntityType = EntityType.Create( "Student", "ns", DataSpace.CSpace, new[] { "StudentId" }, new[] { EdmProperty.Create("StudentId", edmIntTypeUsage) }, null); var conceptualStudentEntitySet = EntitySet.Create("StudentSet", "dbo", "Students", null, conceptualStudentEntityType, null); var conceptualTeachersEndMember = AssociationEndMember.Create( "TeachersEnd", conceptualTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var conceptualStudentsEndMember = AssociationEndMember.Create( "StudentsEnd", conceptualStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var conceptualAssociationType = AssociationType.Create( "TeacherStudentAssociation", "ns.Model", false, DataSpace.CSpace, conceptualTeachersEndMember, conceptualStudentsEndMember, new ReferentialConstraint( conceptualTeachersEndMember, conceptualStudentsEndMember, conceptualTeacherEntityType.KeyProperties, conceptualStudentEntityType.KeyProperties), null); var conceptualAssociationSet = AssociationSet.Create( "TeacherStudentSet", conceptualAssociationType, conceptualTeacherEntitySet, conceptualStudentEntitySet, null); #endregion var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true); mappingContext.AddMapping(collapsedAssociationSet, conceptualAssociationSet); mappingContext.AddMapping(storeTeachersEndMember, conceptualTeachersEndMember); mappingContext.AddMapping(storeStudentsEndMember, conceptualStudentsEndMember); mappingContext.AddMapping( storeTeacherAssociationSet.AssociationSetEnds.ElementAt(0), conceptualAssociationSet.AssociationSetEnds.ElementAt(0)); mappingContext.AddMapping( storeStudentAssociationSet.AssociationSetEnds.ElementAt(0), conceptualAssociationSet.AssociationSetEnds.ElementAt(1)); mappingContext.AddMapping( storeStudentEntityType.KeyProperties.Single(), conceptualStudentEntityType.KeyProperties.Single()); mappingContext.AddMapping( storeTeacherEntityType.KeyProperties.Single(), conceptualTeacherEntityType.KeyProperties.Single()); var storageEntitySetMapping = new EntityContainerMapping(conceptualContainer, null, null, false, false); var associationSetMapping = DbDatabaseMappingBuilder.BuildAssociationSetMappings(storageEntitySetMapping, mappingContext) .SingleOrDefault(); Assert.NotNull(associationSetMapping); var mappingFragment = associationSetMapping.TypeMappings.SingleOrDefault(); Assert.NotNull(mappingFragment); var propertyMappings = mappingFragment.MappingFragments.Single().PropertyMappings; Assert.Equal(2, propertyMappings.Count); Assert.Same(conceptualTeachersEndMember, ((EndPropertyMapping)propertyMappings[0]).AssociationEnd); Assert.Same(conceptualStudentsEndMember, ((EndPropertyMapping)propertyMappings[1]).AssociationEnd); var scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[0]).PropertyMappings.Single(); Assert.Same(conceptualTeacherEntityType.KeyMembers.Single(), scalarPropertyMapping.Property); Assert.Same( joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinTeacherId"), scalarPropertyMapping.Column); scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[1]).PropertyMappings.Single(); Assert.Same(conceptualStudentEntityType.KeyMembers.Single(), scalarPropertyMapping.Property); Assert.Same( joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinStudentId"), scalarPropertyMapping.Column); }
private void GenerateAssociationSet( SimpleMappingContext mappingContext, CollapsibleEntityAssociationSets collapsibleItem, UniqueIdentifierService uniqueEntityContainerNames, UniqueIdentifierService globallyUniqueTypeNames) { var uniqueEndMemberNames = new UniqueIdentifierService(StringComparer.OrdinalIgnoreCase); var associationSetEndDetails0 = collapsibleItem.GetStoreAssociationSetEnd(0); var associationEndMember0 = GenerateAssociationEndMember( mappingContext, associationSetEndDetails0.AssociationSetEnd.CorrespondingAssociationEndMember, uniqueEndMemberNames, associationSetEndDetails0.Multiplicity, associationSetEndDetails0.DeleteBehavior); var conceptualEntitySet0 = mappingContext[associationSetEndDetails0.AssociationSetEnd.EntitySet]; var associationSetEndDetails1 = collapsibleItem.GetStoreAssociationSetEnd(1); var associationEndMember1 = GenerateAssociationEndMember( mappingContext, associationSetEndDetails1.AssociationSetEnd.CorrespondingAssociationEndMember, uniqueEndMemberNames, associationSetEndDetails1.Multiplicity, associationSetEndDetails1.DeleteBehavior); var conceptualEntitySet1 = mappingContext[associationSetEndDetails1.AssociationSetEnd.EntitySet]; globallyUniqueTypeNames.UnregisterIdentifier(mappingContext[collapsibleItem.EntitySet.ElementType].Name); uniqueEntityContainerNames.UnregisterIdentifier(mappingContext[collapsibleItem.EntitySet].Name); var associationTypeName = CreateModelName(collapsibleItem.EntitySet.Name, globallyUniqueTypeNames); var associationSetName = CreateModelName(collapsibleItem.EntitySet.Name, uniqueEntityContainerNames); var conceptualAssociationType = AssociationType.Create( associationTypeName, _namespaceName, false, DataSpace.CSpace, associationEndMember0, associationEndMember1, null, // Don't need a referential constraint. null); CreateModelNavigationProperties(conceptualAssociationType); var conceptualAssociationSet = AssociationSet.Create( associationSetName, conceptualAssociationType, conceptualEntitySet0, conceptualEntitySet1, null); Debug.Assert(conceptualAssociationSet.AssociationSetEnds.Count == 2); var conceptualSetEnd0 = conceptualAssociationSet.AssociationSetEnds[0]; var conceptualSetEnd1 = conceptualAssociationSet.AssociationSetEnds[1]; mappingContext.AddMapping(associationSetEndDetails0.AssociationSetEnd, conceptualSetEnd0); mappingContext.AddMapping(associationSetEndDetails1.AssociationSetEnd, conceptualSetEnd1); mappingContext.AddMapping(collapsibleItem, conceptualAssociationSet); mappingContext.RemoveMapping(collapsibleItem.EntitySet); }
private static AssociationSetMapping BuildAssociationSetMapping( CollapsibleEntityAssociationSets collapsibleItem, EntityContainerMapping entityContainerMapping, SimpleMappingContext mappingContext) { Debug.Assert(collapsibleItem != null, "collapsibleItem != null"); Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null"); Debug.Assert(mappingContext != null, "mappingContext != null"); var modelAssociationSet = mappingContext[collapsibleItem]; if (modelAssociationSet.ElementType.IsForeignKey) { return null; } var associationSetMapping = new AssociationSetMapping( modelAssociationSet, collapsibleItem.EntitySet, entityContainerMapping); var count = collapsibleItem.AssociationSets.Count; if (count > 0) { associationSetMapping.SourceEndMapping = BuildEndPropertyMapping( collapsibleItem.GetStoreAssociationSetEnd(0).AssociationSetEnd, mappingContext); } if (count > 1) { associationSetMapping.TargetEndMapping = BuildEndPropertyMapping( collapsibleItem.GetStoreAssociationSetEnd(1).AssociationSetEnd, mappingContext); } return associationSetMapping; }
public void AddMapping( CollapsibleEntityAssociationSets collapsedAssociationSet, AssociationSet conceptualAssociationSet) { _collapsedAssociationSetMappings.Add(collapsedAssociationSet, conceptualAssociationSet); }
public AssociationSet this[CollapsibleEntityAssociationSets collapsedAssociationSet] { get { return(_collapsedAssociationSetMappings[collapsedAssociationSet]); } }
public AssociationSet this[CollapsibleEntityAssociationSets collapsedAssociationSet] { get { return _collapsedAssociationSetMappings[collapsedAssociationSet]; } }
public void BuildAssociationSetMappings_builds_conceptual_assocation_set_mapping_for_collapsed_store_entity_sets() { #region Setting up many to many relationship in the SSpace Teacher * -- 1 TeacherStudents 1 -- * Teachers var joinStoreEntityType = EntityType.Create( "TeacherStudents", "ns.Store", DataSpace.SSpace, new[] { "JoinTeacherId", "JoinStudentId" }, new[] { CreateStoreProperty("JoinTeacherId", "int"), CreateStoreProperty("JoinStudentId", "int") }, null); var joinStoreEntitySet = EntitySet.Create("TeacherStudentsSet", "dbo", "TeacherStudentTable", null, joinStoreEntityType, null); var storeTeacherEntityType = EntityType.Create( "Teacher", "ns.Store", DataSpace.SSpace, new[] { "TeacherId" }, new[] { CreateStoreProperty("TeacherId", "int") }, null); var storeTeacherEntitySet = EntitySet.Create("TeachersSet", "dbo", "Teachers", null, storeTeacherEntityType, null); var storeStudentEntityType = EntityType.Create( "Student", "ns.Store", DataSpace.SSpace, new[] { "StudentId" }, new[] { CreateStoreProperty("StudentId", "int") }, null); var storeStudentEntitySet = EntitySet.Create("StudentSet", "dbo", "Students", null, storeStudentEntityType, null); var storeTeachersEndMember = AssociationEndMember.Create( "Teachers", storeTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var storeTeacherStudentsfromTeachersEndMember = AssociationEndMember.Create( "TeacherStudents_fromTeachers", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null); var storeTeacherAssociationType = AssociationType.Create( "Teacher_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace, storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, new ReferentialConstraint( storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, storeTeacherEntityType.KeyProperties, joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinTeacherId")), null); var storeTeacherAssociationSet = AssociationSet.Create( "Teacher_TeacherStudents", storeTeacherAssociationType, storeTeacherEntitySet, joinStoreEntitySet, null); var storeStudentsEndMember = AssociationEndMember.Create( "Students", storeStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var storeTeacherStudentsfromStudentsEndMember = AssociationEndMember.Create( "TeacherStudents_fromStudents", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One, OperationAction.None, null); var storeStudentAssociationType = AssociationType.Create( "Student_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace, storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, new ReferentialConstraint( storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, storeStudentEntityType.KeyProperties, joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinStudentId")), null); var storeStudentAssociationSet = AssociationSet.Create( "Student_TeacherStudents", storeStudentAssociationType, storeStudentEntitySet, joinStoreEntitySet, null); var collapsedAssociationSet = new CollapsibleEntityAssociationSets(joinStoreEntitySet); collapsedAssociationSet.AssociationSets.Add(storeTeacherAssociationSet); collapsedAssociationSet.AssociationSets.Add(storeStudentAssociationSet); #endregion #region Setting up many to many relationship in the CSpace Teacher * -- * Teachers var conceptualContainer = EntityContainer.Create("ConceptualContainer", DataSpace.CSpace, null, null, null); var edmIntTypeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); var conceptualTeacherEntityType = EntityType.Create( "Teacher", "ns", DataSpace.CSpace, new[] { "TeacherId" }, new[] { EdmProperty.Create("TeacherId", edmIntTypeUsage) }, null); var conceptualTeacherEntitySet = EntitySet.Create("TeachersSet", null, null, null, conceptualTeacherEntityType, null); var conceptualStudentEntityType = EntityType.Create( "Student", "ns", DataSpace.CSpace, new[] { "StudentId" }, new[] { EdmProperty.Create("StudentId", edmIntTypeUsage) }, null); var conceptualStudentEntitySet = EntitySet.Create("StudentSet", "dbo", "Students", null, conceptualStudentEntityType, null); var conceptualTeachersEndMember = AssociationEndMember.Create( "TeachersEnd", conceptualTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var conceptualStudentsEndMember = AssociationEndMember.Create( "StudentsEnd", conceptualStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many, OperationAction.None, null); var conceptualAssociationType = AssociationType.Create( "TeacherStudentAssociation", "ns.Model", false, DataSpace.CSpace, conceptualTeachersEndMember, conceptualStudentsEndMember, new ReferentialConstraint( conceptualTeachersEndMember, conceptualStudentsEndMember, conceptualTeacherEntityType.KeyProperties, conceptualStudentEntityType.KeyProperties), null); var conceptualAssociationSet = AssociationSet.Create( "TeacherStudentSet", conceptualAssociationType, conceptualTeacherEntitySet, conceptualStudentEntitySet, null); #endregion var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true); mappingContext.AddMapping(collapsedAssociationSet, conceptualAssociationSet); mappingContext.AddMapping(storeTeachersEndMember, conceptualTeachersEndMember); mappingContext.AddMapping(storeStudentsEndMember, conceptualStudentsEndMember); mappingContext.AddMapping( storeTeacherAssociationSet.AssociationSetEnds.ElementAt(0), conceptualAssociationSet.AssociationSetEnds.ElementAt(0)); mappingContext.AddMapping( storeStudentAssociationSet.AssociationSetEnds.ElementAt(0), conceptualAssociationSet.AssociationSetEnds.ElementAt(1)); mappingContext.AddMapping( storeStudentEntityType.KeyProperties.Single(), conceptualStudentEntityType.KeyProperties.Single()); mappingContext.AddMapping( storeTeacherEntityType.KeyProperties.Single(), conceptualTeacherEntityType.KeyProperties.Single()); var storageEntitySetMapping = new EntityContainerMapping(conceptualContainer, null, null, false, false); var associationSetMapping = DbDatabaseMappingBuilder.BuildAssociationSetMappings(storageEntitySetMapping, mappingContext) .SingleOrDefault(); Assert.NotNull(associationSetMapping); var mappingFragment = associationSetMapping.TypeMappings.SingleOrDefault(); Assert.NotNull(mappingFragment); var propertyMappings = mappingFragment.MappingFragments.Single().PropertyMappings; Assert.Equal(2, propertyMappings.Count); Assert.Same(conceptualTeachersEndMember, ((EndPropertyMapping)propertyMappings[0]).AssociationEnd); Assert.Same(conceptualStudentsEndMember, ((EndPropertyMapping)propertyMappings[1]).AssociationEnd); var scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[0]).PropertyMappings.Single(); Assert.Same(conceptualTeacherEntityType.KeyMembers.Single(), scalarPropertyMapping.Property); Assert.Same( joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinTeacherId"), scalarPropertyMapping.Column); scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[1]).PropertyMappings.Single(); Assert.Same(conceptualStudentEntityType.KeyMembers.Single(), scalarPropertyMapping.Property); Assert.Same( joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinStudentId"), scalarPropertyMapping.Column); }