public void AddEntityTypeMappingFragment( EdmEntitySet entitySet, EdmEntityType entityType, DbEntityTypeMappingFragment fragment) { Contract.Assert(fragment.Table == Table); _entityTypes.Add(entitySet, entityType); var defaultDiscriminatorColumn = fragment.GetDefaultDiscriminator(); DbColumnCondition defaultDiscriminatorCondition = null; if (defaultDiscriminatorColumn != null) { defaultDiscriminatorCondition = fragment.ColumnConditions.SingleOrDefault(cc => cc.Column == defaultDiscriminatorColumn); } foreach (var pm in fragment.PropertyMappings) { var columnMapping = FindOrCreateColumnMapping(pm.Column); columnMapping.AddMapping( entityType, pm.PropertyPath, fragment.ColumnConditions.Where(cc => cc.Column == pm.Column), defaultDiscriminatorColumn == pm.Column); } // Add any column conditions that aren't mapped to properties foreach ( var cc in fragment.ColumnConditions.Where(cc => !fragment.PropertyMappings.Any(pm => pm.Column == cc.Column))) { var columnMapping = FindOrCreateColumnMapping(cc.Column); columnMapping.AddMapping(entityType, null, new[] { cc }, defaultDiscriminatorColumn == cc.Column); } }
public void Add(EdmEntitySet entitySet, EdmEntityType entityType) { Contract.Requires(entitySet != null); Contract.Requires(entityType != null); var i = 0; List<EdmEntityType> entityTypes; if (!_entityTypes.TryGetValue(entitySet, out entityTypes)) { entityTypes = new List<EdmEntityType>(); _entityTypes.Add(entitySet, entityTypes); } for (; i < entityTypes.Count; i++) { if (entityTypes[i] == entityType) { return; } else if (entityType.IsAncestorOf(entityTypes[i])) { break; } } entityTypes.Insert(i, entityType); }
public bool Contains(EdmEntitySet entitySet, EdmEntityType entityType) { Contract.Requires(entitySet != null); Contract.Requires(entityType != null); List<EdmEntityType> setTypes; return _entityTypes.TryGetValue(entitySet, out setTypes) && setTypes.Contains(entityType); }
public void Can_get_and_set_configuration_annotation() { var entitySet = new EdmEntitySet(); entitySet.SetConfiguration(42); Assert.Equal(42, entitySet.GetConfiguration()); }
public void AddEntitySetMapping_should_add_mapping() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel().Initialize(), new DbDatabaseMetadata()); var entitySet = new EdmEntitySet(); var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet); Assert.NotNull(entitySetMapping); Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().EntitySetMappings.Count()); Assert.Same(entitySet, entitySetMapping.EntitySet); }
public IEnumerable<EdmEntityType> GetEntityTypes(EdmEntitySet entitySet) { List<EdmEntityType> entityTypes; if (_entityTypes.TryGetValue(entitySet, out entityTypes)) { return entityTypes; } else { return _emptyTypes; } }
public bool IsRoot(EdmEntitySet entitySet, EdmEntityType entityType) { Contract.Requires(entitySet != null); Contract.Requires(entityType != null); var isRoot = true; var entityTypes = _entityTypes[entitySet]; foreach (var et in entityTypes) { if (et != entityType && et.IsAncestorOf(entityType)) { isRoot = false; } } return isRoot; }
public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel().Initialize(), new DbDatabaseMetadata()); var entitySet = new EdmEntitySet(); var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet); var entityTypeMapping = new DbEntityTypeMapping(); entitySetMapping.EntityTypeMappings.Add(entityTypeMapping); var entityTypeMappingFragment = new DbEntityTypeMappingFragment(); entityTypeMapping.TypeMappingFragments.Add(entityTypeMappingFragment); var propertyMapping1 = new DbEdmPropertyMapping(); var complexType = new EdmComplexType(); complexType.SetClrType(typeof(object)); propertyMapping1.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } }); entityTypeMappingFragment.PropertyMappings.Add(propertyMapping1); var propertyMapping2 = new DbEdmPropertyMapping(); propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference() }); propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } }); entityTypeMappingFragment.PropertyMappings.Add(propertyMapping2); Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count()); }
private void RemoveFragment( EdmEntitySet entitySet, DbEntityTypeMapping entityTypeMapping, DbEntityTypeMappingFragment fragment) { // Make the default discriminator nullable if this type isn't using it but there is a base type var defaultDiscriminator = fragment.GetDefaultDiscriminator(); if (defaultDiscriminator != null && entityTypeMapping.EntityType.BaseType != null) { var columnMapping = _tableMappings[fragment.Table].ColumnMappings.SingleOrDefault( cm => cm.Column == defaultDiscriminator); if (columnMapping != null) { var propertyMapping = columnMapping.PropertyMappings.SingleOrDefault( pm => pm.EntityType == entityTypeMapping.EntityType); if (propertyMapping != null) { columnMapping.PropertyMappings.Remove(propertyMapping); } } defaultDiscriminator.IsNullable = true; } entityTypeMapping.TypeMappingFragments.Remove(fragment); if (!entityTypeMapping.TypeMappingFragments.Any()) { _databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Remove(entityTypeMapping); } }
/// <summary> /// Determines if the table and entity type need mapping, and if not, removes the existing entity type mapping /// </summary> private bool FindPropertyEntityTypeMapping( TableMapping tableMapping, EdmEntitySet entitySet, EdmEntityType entityType, bool requiresIsTypeOf, out DbEntityTypeMapping entityTypeMapping, out DbEntityTypeMappingFragment fragment) { entityTypeMapping = null; fragment = null; var mapping = (from etm in _databaseMapping.GetEntityTypeMappings(entityType) from tmf in etm.TypeMappingFragments where tmf.Table == tableMapping.Table select new { TypeMapping = etm, Fragment = tmf }).SingleOrDefault(); if (mapping != null) { entityTypeMapping = mapping.TypeMapping; fragment = mapping.Fragment; if (!requiresIsTypeOf && entityType.IsAbstract) { RemoveFragment(entitySet, mapping.TypeMapping, mapping.Fragment); return false; } return true; } else { return false; } }
private bool DetermineRequiresIsTypeOf( TableMapping tableMapping, EdmEntitySet entitySet, EdmEntityType entityType) { // IsTypeOf if this is the root for this table and any derived type shares a property mapping return entityType.IsRootOfSet(tableMapping.EntityTypes.GetEntityTypes(entitySet)) && ((tableMapping.EntityTypes.GetEntityTypes(entitySet).Count() > 1 && tableMapping.EntityTypes.GetEntityTypes(entitySet).Any(et => et != entityType && !et.IsAbstract)) || _tableMappings.Values.Any( tm => tm != tableMapping && tm.Table.ForeignKeyConstraints.Any( fk => fk.GetIsTypeConstraint() && fk.PrincipalTable == tableMapping.Table))); }
private void MarkColumnsAsNonNullableIfNoTableSharing( EdmEntitySet entitySet, DbTableMetadata table, EdmEntityType dependentEndEntityType, IEnumerable<DbTableColumnMetadata> columns) { // determine if base entities share this table, if not, the foreign keys can be non-nullable var mappedBaseTypes = _tableMappings[table].EntityTypes.GetEntityTypes(entitySet).Where( et => et != dependentEndEntityType && (et.IsAncestorOf(dependentEndEntityType) || !dependentEndEntityType.IsAncestorOf(et))); if (mappedBaseTypes.Count() == 0 || mappedBaseTypes.All(et => et.IsAbstract)) { columns.Each(c => c.IsNullable = false); } }
/// <summary> /// Sets nullability for association set mappings' foreign keys for 1:* and 1:0..1 associations /// when no base types share the the association set mapping's table /// </summary> private void ConfigureAssociationSetMappingForeignKeys(EdmEntitySet entitySet) { foreach (var asm in _databaseMapping.EntityContainerMappings .SelectMany(ecm => ecm.AssociationSetMappings) .Where( asm => (asm.AssociationSet.SourceSet == entitySet || asm.AssociationSet.TargetSet == entitySet) && asm.AssociationSet.ElementType.IsRequiredToNonRequired())) { EdmAssociationEnd _, dependentEnd; asm.AssociationSet.ElementType.TryGuessPrincipalAndDependentEnds(out _, out dependentEnd); if ((dependentEnd == asm.AssociationSet.ElementType.SourceEnd && asm.AssociationSet.SourceSet == entitySet) || (dependentEnd == asm.AssociationSet.ElementType.TargetEnd && asm.AssociationSet.TargetSet == entitySet)) { var dependentMapping = asm.SourceEndMapping.AssociationEnd == dependentEnd ? asm.TargetEndMapping : asm.SourceEndMapping; MarkColumnsAsNonNullableIfNoTableSharing( entitySet, asm.Table, dependentEnd.EntityType, dependentMapping.PropertyMappings.Select(pm => pm.Column)); } } }
internal static DbEntitySetMapping AddEntitySetMapping( this DbDatabaseMapping databaseMapping, EdmEntitySet entitySet) { Contract.Requires(databaseMapping != null); Contract.Requires(entitySet != null); var entitySetMapping = new DbEntitySetMapping { EntitySet = entitySet }; databaseMapping .EntityContainerMappings .Single() .EntitySetMappings .Add(entitySetMapping); return entitySetMapping; }
internal static DbEntitySetMapping GetEntitySetMapping( this DbDatabaseMapping databaseMapping, EdmEntitySet entitySet) { Contract.Requires(databaseMapping != null); Contract.Requires(entitySet != null); return databaseMapping .EntityContainerMappings .Single() .EntitySetMappings .SingleOrDefault(e => e.EntitySet == entitySet); }
public void Can_get_and_set_mapping_for_entity_set() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel().Initialize(), new DbDatabaseMetadata()); var entitySet = new EdmEntitySet(); Assert.Same(databaseMapping.AddEntitySetMapping(entitySet), databaseMapping.GetEntitySetMapping(entitySet)); }
public static void ReplaceEntitySet( this EdmModel model, EdmEntityType entityType, EdmEntitySet newSet) { //Contract.Requires(model != null); //Contract.Requires(entityType != null); Contract.Assert(model.Containers.Count == 1); var container = model.Containers.Single(); var entitySet = container.EntitySets.SingleOrDefault(a => a.ElementType == entityType); container.EntitySets.Remove(entitySet); if (entitySet != null && newSet != null) { // Update AssociationSets to point to entitySet instead of derivedEntitySet foreach (var associationSet in model.Containers.Single().AssociationSets) { if (associationSet.SourceSet == entitySet) { associationSet.SourceSet = newSet; } if (associationSet.TargetSet == entitySet) { associationSet.TargetSet = newSet; } } } }
public static EdmEntitySet AddEntitySet( this EdmModel model, string name, EdmEntityType elementType) { //Contract.Requires(model != null); //Contract.Requires(!string.IsNullOrWhiteSpace(name)); //Contract.Requires(elementType != null); Contract.Assert(model.Containers.Count == 1); var entitySet = new EdmEntitySet { Name = name, ElementType = elementType }; model.Containers.Single().EntitySets.Add(entitySet); return entitySet; }