public SimpleMappingContext(EdmModel storeModel, bool includeForeignKeyProperties) { Debug.Assert(storeModel != null, "storeModel != null"); StoreModel = storeModel; IncludeForeignKeyProperties = includeForeignKeyProperties; }
public void Generate_can_map_a_simple_entity_type_and_set() { var model = new EdmModel(DataSpace.CSpace); var entityType = model.AddEntityType("E"); var type = typeof(object); entityType.GetMetadataProperties().SetClrType(type); var property = EdmProperty.CreatePrimitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); var property1 = EdmProperty.CreatePrimitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var entitySet = model.AddEntitySet("ESet", entityType); var databaseMapping = CreateDatabaseMappingGenerator().Generate(model); var entitySetMapping = databaseMapping.GetEntitySetMapping(entitySet); Assert.NotNull(entitySetMapping); Assert.Same(entitySet, entitySetMapping.EntitySet); var entityTypeMapping = entitySetMapping.EntityTypeMappings.Single(); Assert.Same(entityType, entityTypeMapping.EntityType); Assert.NotNull(entityTypeMapping.MappingFragments.Single().Table); Assert.Equal("E", entityTypeMapping.MappingFragments.Single().Table.Name); Assert.Equal(2, entityTypeMapping.MappingFragments.Single().Table.Properties.Count); Assert.Equal(typeof(object), entityTypeMapping.GetClrType()); }
public EdmModelValidationContext(EdmModel model, bool validateSyntax) { DebugCheck.NotNull(model); _model = model; _validateSyntax = validateSyntax; }
public void Configure_should_configure_modification_functions() { var model = new EdmModel(DataSpace.CSpace); var entityType = model.AddEntityType("E"); entityType.GetMetadataProperties().SetClrType(typeof(object)); model.AddEntitySet("ESet", entityType); var modificationFunctionsConfigurationMock = new Mock<ModificationStoredProceduresConfiguration>(); var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object)); entityTypeConfiguration.MapToStoredProcedures(modificationFunctionsConfigurationMock.Object, true); entityType.SetConfiguration(entityTypeConfiguration); var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest).Generate(model); entityTypeConfiguration.Configure(entityType, databaseMapping, ProviderRegistry.Sql2008_ProviderManifest); modificationFunctionsConfigurationMock .Verify( m => m.Configure( It.IsAny<EntityTypeModificationFunctionMapping>(), It.IsAny<DbProviderManifest>()), Times.Once()); }
public void Can_set_owner_and_corresponding_association_added_to_model() { var database = new EdmModel(DataSpace.SSpace); var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK"); var principalTable = database.AddTable("P"); foreignKeyBuilder.PrincipalTable = principalTable; var dependentTable = database.AddTable("D"); foreignKeyBuilder.SetOwner(dependentTable); var associationType = database.GetAssociationType("FK"); Assert.NotNull(associationType); Assert.NotNull(associationType.SourceEnd); Assert.NotNull(associationType.TargetEnd); Assert.Same(principalTable, associationType.SourceEnd.GetEntityType()); Assert.Equal("P", associationType.SourceEnd.Name); Assert.Same(dependentTable, associationType.TargetEnd.GetEntityType()); Assert.Equal("D", associationType.TargetEnd.Name); var associationSet = database.GetAssociationSet(associationType); Assert.NotNull(associationSet); Assert.NotNull(associationSet.SourceSet); Assert.NotNull(associationSet.TargetSet); }
public void Apply_should_not_match_key_that_is_also_an_fk() { var model = new EdmModel(DataSpace.CSpace); var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64)); entityType.AddKeyMember(property); var associationType = model.AddAssociationType( "A", new EntityType("E", "N", DataSpace.CSpace), RelationshipMultiplicity.ZeroOrOne, entityType, RelationshipMultiplicity.Many); associationType.Constraint = new ReferentialConstraint( associationType.SourceEnd, associationType.TargetEnd, new[] { property }, new[] { property }); (new StoreGeneratedIdentityKeyConvention()) .Apply(entityType, new DbModel(model, null)); Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern()); }
public void HasCascadeDeletePath_should_return_true_for_transitive_cascade() { var model = new EdmModel(DataSpace.CSpace); var entityTypeA = model.AddEntityType("A"); var entityTypeB = model.AddEntityType("B"); var entityTypeC = model.AddEntityType("B"); var associationTypeA = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace) { SourceEnd = new AssociationEndMember("S", entityTypeA), TargetEnd = new AssociationEndMember("T", entityTypeB) }; associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade; model.AddAssociationType(associationTypeA); var associationTypeB = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace) { SourceEnd = new AssociationEndMember("S", entityTypeB), TargetEnd = new AssociationEndMember("T", entityTypeC) }; associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade; model.AddAssociationType(associationTypeB); Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB)); Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC)); Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC)); Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA)); Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB)); Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA)); }
public void Configure_should_uniquify_unconfigured_assocation_function_names() { var typeA = new MockType("A"); var typeB = new MockType("B").Property(typeA.AsCollection(), "As"); var mockPropertyInfo = typeB.GetProperty("As"); typeA.Property(typeB.AsCollection(), "Bs"); var modelConfiguration = new ModelConfiguration(); var navigationPropertyConfiguration = modelConfiguration.Entity(typeB).Navigation(mockPropertyInfo); navigationPropertyConfiguration.ModificationFunctionsConfiguration = new ModificationFunctionsConfiguration(); var modificationFunctionConfiguration = new ModificationFunctionConfiguration(); modificationFunctionConfiguration.HasName("M2M_Delete"); navigationPropertyConfiguration.ModificationFunctionsConfiguration .Insert(modificationFunctionConfiguration); var model = new EdmModel(DataSpace.CSpace); var entityA = model.AddEntityType("A"); entityA.Annotations.SetClrType(typeA); entityA.SetConfiguration(modelConfiguration.Entity(typeA)); var entityB = model.AddEntityType("B"); entityB.Annotations.SetClrType(typeB); entityB.SetConfiguration(modelConfiguration.Entity(typeB)); model.AddEntitySet("AS", entityA); model.AddEntitySet("BS", entityB); var associationType = model.AddAssociationType( "M2M", entityA, RelationshipMultiplicity.Many, entityB, RelationshipMultiplicity.Many); associationType.SetConfiguration(navigationPropertyConfiguration); var navigationProperty = entityB.AddNavigationProperty("As", associationType); navigationProperty.SetClrPropertyInfo(mockPropertyInfo); model.AddAssociationSet("M2MSet", associationType); var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest) .Generate(model); modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest); Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete")); Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete1")); }
public void Generate_can_map_a_simple_entity_type_and_set() { var model = new EdmModel().Initialize(); var entityType = model.AddEntityType("E"); var type = typeof(object); entityType.Annotations.SetClrType(type); var property = EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); var property1 = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var entitySet = model.AddEntitySet("ESet", entityType); var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(model); var entitySetMapping = databaseMapping.GetEntitySetMapping(entitySet); Assert.NotNull(entitySetMapping); Assert.Same(entitySet, entitySetMapping.EntitySet); var entityTypeMapping = entitySetMapping.EntityTypeMappings.Single(); Assert.Same(entityType, entityTypeMapping.EntityType); Assert.NotNull(entityTypeMapping.MappingFragments.Single().Table); Assert.Equal("E", entityTypeMapping.MappingFragments.Single().Table.Name); Assert.Equal(2, entityTypeMapping.MappingFragments.Single().Table.Properties.Count); Assert.Equal(typeof(object), entityTypeMapping.GetClrType()); }
public void Configure_should_configure_inverse() { var inverseMockPropertyInfo = new MockPropertyInfo(); var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo()) { InverseNavigationProperty = inverseMockPropertyInfo }; var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); var inverseAssociationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); var model = new EdmModel(DataSpace.CSpace); model.AddAssociationType(inverseAssociationType); var inverseNavigationProperty = model.AddEntityType("T") .AddNavigationProperty("N", inverseAssociationType); inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo); navigationPropertyConfiguration.Configure( new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType())) { RelationshipType = associationType }, model, new EntityTypeConfiguration(typeof(object))); Assert.Same(associationType, inverseNavigationProperty.Association); Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd); Assert.Same(associationType.TargetEnd, inverseNavigationProperty.FromEndMember); Assert.Equal(0, model.AssociationTypes.Count()); }
public void Configure_should_rename_columns_when_right_keys_configured() { var database = new EdmModel(DataSpace.CSpace); var associationSetMapping = new StorageAssociationSetMapping( new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), new EntitySet()) .Initialize(); var column = new EdmProperty("C"); associationSetMapping.TargetEndMapping.AddProperty(new StorageScalarPropertyMapping(new EdmProperty("PK"), column)); var manyToManyAssociationMappingConfiguration = new ManyToManyAssociationMappingConfiguration(); manyToManyAssociationMappingConfiguration.MapRightKey("NewName"); var mockPropertyInfo = new MockPropertyInfo(); associationSetMapping.SourceEndMapping.EndMember = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationSetMapping.SourceEndMapping.EndMember.SetClrPropertyInfo(mockPropertyInfo); manyToManyAssociationMappingConfiguration.Configure(associationSetMapping, database, mockPropertyInfo); Assert.Equal("NewName", column.Name); }
public void Configure_should_rename_table_when_table_configured() { var database = new EdmModel(DataSpace.SSpace); var table = database.AddTable("OriginalName"); var associationSetMapping = new StorageAssociationSetMapping( new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), database.GetEntitySet(table)) .Initialize(); var manyToManyAssociationMappingConfiguration = new ManyToManyAssociationMappingConfiguration(); manyToManyAssociationMappingConfiguration.ToTable("NewName"); var mockPropertyInfo = new MockPropertyInfo(); associationSetMapping.SourceEndMapping.EndMember = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace)); associationSetMapping.SourceEndMapping.EndMember.SetClrPropertyInfo(mockPropertyInfo); manyToManyAssociationMappingConfiguration.Configure(associationSetMapping, database, mockPropertyInfo); Assert.Equal("NewName", table.GetTableName().Name); Assert.Same(manyToManyAssociationMappingConfiguration, table.GetConfiguration()); }
public void Map_should_create_association_sets_for_associations() { var modelConfiguration = new ModelConfiguration(); var model = new EdmModel().Initialize(); var entityType = new EntityType { Name = "Source" }; model.AddEntitySet("Source", entityType); var mappingContext = new MappingContext(modelConfiguration, new ConventionsConfiguration(), model); new NavigationPropertyMapper(new TypeMapper(mappingContext)) .Map( new MockPropertyInfo(new MockType("Target"), "Nav"), entityType, () => new EntityTypeConfiguration(typeof(object))); Assert.Equal(1, model.Containers.Single().AssociationSets.Count); var associationSet = model.Containers.Single().AssociationSets.Single(); Assert.NotNull(associationSet); Assert.NotNull(associationSet.ElementType); Assert.Equal("Source_Nav", associationSet.Name); }
public void GenerateModel_genertes_model_and_sets_all_the_properties() { var mockModelGenerator = new Mock<ModelGenerator>(new ModelBuilderSettings(), "storeNamespace"); var storeModel = new EdmModel(DataSpace.SSpace); var mappingContext = new SimpleMappingContext(storeModel, true); mappingContext.AddMapping( storeModel.Containers.Single(), EntityContainer.Create("C", DataSpace.CSpace, null, null, null)); mockModelGenerator .Setup(g => g.CreateStoreModel()) .Returns(() => storeModel); mockModelGenerator .Setup(g => g.CreateMappingContext(It.Is<EdmModel>(model => model == storeModel))) .Returns(() => mappingContext); var errors = new List<EdmSchemaError>(); var databaseMapping = mockModelGenerator.Object.GenerateModel(errors).DatabaseMapping; Assert.Same(storeModel, databaseMapping.Database); Assert.NotNull(databaseMapping.Model); Assert.Equal(1, databaseMapping.EntityContainerMappings.Count); mockModelGenerator.Verify( g => g.CreateMappingContext(It.IsAny<EdmModel>()), Times.Once()); Assert.Empty(errors); }
public void Store_model_initialized() { var storeModel = new EdmModel(DataSpace.SSpace); var mappingContext = new SimpleMappingContext(storeModel, true); Assert.Same(storeModel, mappingContext.StoreModel); Assert.True(mappingContext.IncludeForeignKeyProperties); }
/// <summary> /// Serialize the <see cref="EdmModel" /> to the XmlWriter. /// </summary> /// <param name="model"> /// The EdmModel to serialize, mut have only one <see cref="EdmNamespace" /> and one /// <see /// cref="Core.Metadata.Edm.EntityContainer" /> /// </param> /// <param name="xmlWriter"> The XmlWriter to serialize to </param> public bool Serialize(EdmModel model, XmlWriter xmlWriter) { Check.NotNull(model, "model"); Check.NotNull(xmlWriter, "xmlWriter"); if (model.Namespaces.Count != 1 || model.Containers.Count != 1) { Validator_OnError( this, new DataModelErrorEventArgs { ErrorMessage = Strings.Serializer_OneNamespaceAndOneContainer, }); } // validate the model first var validator = new DataModelValidator(); validator.OnError += Validator_OnError; validator.Validate(model, true); if (_isModelValid) { var visitor = new EdmSerializationVisitor(xmlWriter, model.Version); visitor.Visit(model); } return _isModelValid; }
public void GlobalItems_should_return_namespace_items_and_containers() { var model = new EdmModel(DataSpace.SSpace); model.AddItem(new EntityType("Entity", "Model", DataSpace.SSpace)); Assert.Equal(2, model.GlobalItems.Count()); }
public void Name_should_return_association_type_name() { var database = new EdmModel(DataSpace.SSpace); var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK"); Assert.Equal("FK", foreignKeyBuilder.Name); }
internal void Visit(EdmModel model) { DebugCheck.NotNull(model); EvaluateItem(model); VisitEdmModel(model); }
public void Validate_should_throw_on_error() { var model = new EdmModel(DataSpace.CSpace); model.AddItem(new EntityType("E", "N", DataSpace.CSpace)); Assert.Throws<ModelValidationException>(() => model.Validate()); }
public void Apply(EdmModel model) { Check.NotNull(model, "model"); var associationPairs = (from a1 in model.AssociationTypes from a2 in model.AssociationTypes where a1 != a2 where a1.SourceEnd.GetEntityType() == a2.TargetEnd.GetEntityType() && a1.TargetEnd.GetEntityType() == a2.SourceEnd.GetEntityType() let a1Configuration = a1.GetConfiguration() as NavigationPropertyConfiguration let a2Configuration = a2.GetConfiguration() as NavigationPropertyConfiguration where (((a1Configuration == null) || ((a1Configuration.InverseEndKind == null) && (a1Configuration.InverseNavigationProperty == null))) && ((a2Configuration == null) || ((a2Configuration.InverseEndKind == null) && (a2Configuration.InverseNavigationProperty == null)))) select new { a1, a2 }) .Distinct((a, b) => a.a1 == b.a2 && a.a2 == b.a1) .GroupBy( (a, b) => a.a1.SourceEnd.GetEntityType() == b.a2.TargetEnd.GetEntityType() && a.a1.TargetEnd.GetEntityType() == b.a2.SourceEnd.GetEntityType()) .Where(g => g.Count() == 1) .Select(g => g.Single()); foreach (var pair in associationPairs) { var unifiedAssociation = pair.a2.GetConfiguration() != null ? pair.a2 : pair.a1; var redundantAssociation = unifiedAssociation == pair.a1 ? pair.a2 : pair.a1; unifiedAssociation.SourceEnd.RelationshipMultiplicity = redundantAssociation.TargetEnd.RelationshipMultiplicity; if (redundantAssociation.Constraint != null) { unifiedAssociation.Constraint = redundantAssociation.Constraint; unifiedAssociation.Constraint.FromRole = unifiedAssociation.SourceEnd; unifiedAssociation.Constraint.ToRole = unifiedAssociation.TargetEnd; } var sourceEndClrProperty = redundantAssociation.SourceEnd.GetClrPropertyInfo(); if (sourceEndClrProperty != null) { unifiedAssociation.TargetEnd.SetClrPropertyInfo(sourceEndClrProperty); } FixNavigationProperties(model, unifiedAssociation, redundantAssociation); model.RemoveAssociationType(redundantAssociation); } }
public void AddTable_should_create_and_add_table_to_default_schema() { var database = new EdmModel(DataSpace.SSpace); var table = database.AddTable("T"); Assert.True(database.EntityTypes.Contains(table)); Assert.Equal("T", database.EntityTypes.First().Name); }
public void Can_get_and_set_provider_annotation() { var database = new EdmModel(DataSpace.CSpace); var providerInfo = new DbProviderInfo("Foo", "Bar"); database.ProviderInfo = providerInfo; Assert.Same(providerInfo, database.ProviderInfo); }
public void AddFunction_should_create_and_add_function_to_model() { var database = new EdmModel(DataSpace.SSpace); var function = database.AddFunction("F", new EdmFunctionPayload()); Assert.True(database.Functions.Contains(function)); Assert.Equal("F", database.Functions.First().Name); }
private DbDatabaseMapping InitializeDatabaseMapping(EdmModel conceptualModel) { DebugCheck.NotNull(conceptualModel); var storeModel = EdmModel.CreateStoreModel( _providerInfo, _providerManifest, conceptualModel.SchemaVersion); return new DbDatabaseMapping().Initialize(conceptualModel, storeModel); }
public void Can_get_and_set_provider_info_annotation() { var model = new EdmModel(DataSpace.SSpace); var providerInfo = ProviderRegistry.Sql2008_ProviderInfo; model.ProviderInfo = providerInfo; Assert.Same(providerInfo, model.ProviderInfo); }
public void Configure_should_uniquify_unconfigured_association_function_names() { var mockPropertyInfo = typeof(BType1).GetDeclaredProperty("As"); var modelConfiguration = new ModelConfiguration(); var navigationPropertyConfiguration = modelConfiguration.Entity(typeof(BType1)).Navigation(mockPropertyInfo); navigationPropertyConfiguration.ModificationStoredProceduresConfiguration = new ModificationStoredProceduresConfiguration(); var modificationFunctionConfiguration = new ModificationStoredProcedureConfiguration(); modificationFunctionConfiguration.HasName("AB_Delete"); navigationPropertyConfiguration.ModificationStoredProceduresConfiguration .Insert(modificationFunctionConfiguration); var model = new EdmModel(DataSpace.CSpace); var entityA = model.AddEntityType("A"); entityA.GetMetadataProperties().SetClrType(typeof(AType1)); entityA.SetConfiguration(modelConfiguration.Entity(typeof(AType1))); var entityB = model.AddEntityType("B"); entityB.GetMetadataProperties().SetClrType(typeof(BType1)); entityB.SetConfiguration(modelConfiguration.Entity(typeof(BType1))); model.AddEntitySet("AS", entityA); model.AddEntitySet("BS", entityB); var associationType = model.AddAssociationType( "M2M", entityA, RelationshipMultiplicity.Many, entityB, RelationshipMultiplicity.Many); associationType.SetConfiguration(navigationPropertyConfiguration); var navigationProperty = entityB.AddNavigationProperty("As", associationType); navigationProperty.SetClrPropertyInfo(mockPropertyInfo); model.AddAssociationSet("M2MSet", associationType); var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest) .Generate(model); modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest); Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete")); Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete1")); }
internal static void Validate(EdmModel validateRoot, EdmModelValidationContext context) { var edmModelValidationRuleSet = EdmModelRuleSet.CreateEdmModelRuleSet(context.ValidationContextVersion, context.ValidateSyntax); var modelVisitor = new EdmModelValidationVisitor(context, edmModelValidationRuleSet); modelVisitor.Visit(validateRoot); }
public void Generate_should_initialize_mapping_model() { var model = new EdmModel(DataSpace.CSpace); var databaseMapping = CreateDatabaseMappingGenerator().Generate(model); Assert.NotNull(databaseMapping); Assert.NotNull(databaseMapping.Database); Assert.Same(model.Containers.Single(), databaseMapping.EntityContainerMappings.Single().EdmEntityContainer); }
public void MapComplexType_should_not_map_invalid_structural_type() { var model = new EdmModel(DataSpace.CSpace); var typeMapper = new TypeMapper(new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), model)); Assert.Null(typeMapper.MapComplexType(typeof(string))); }