Exemplo n.º 1
0
        /// <inheritdoc />
        public virtual void Apply(EntityType item, DbModel model)
        {
            Check.NotNull <EntityType>(item, nameof(item));
            Check.NotNull <DbModel>(model, nameof(model));
            if (item.BaseType != null || item.KeyProperties.Count != 1 || item.DeclaredProperties.Select(p => new
            {
                p = p,
                sgp = p.GetStoreGeneratedPattern()
            }).Where(_param0 =>
            {
                if (!_param0.sgp.HasValue)
                {
                    return(false);
                }
                StoreGeneratedPattern?sgp = _param0.sgp;
                if (sgp.GetValueOrDefault() == StoreGeneratedPattern.Identity)
                {
                    return(sgp.HasValue);
                }
                return(false);
            }).Select(_param0 => _param0.sgp).Any <StoreGeneratedPattern?>())
            {
                return;
            }
            EdmProperty property = item.KeyProperties.Single <EdmProperty>();

            if (property.GetStoreGeneratedPattern().HasValue || property.PrimitiveType == null || !StoreGeneratedIdentityKeyConvention._applicableTypes.Contains <PrimitiveTypeKind>(property.PrimitiveType.PrimitiveTypeKind) || (model.ConceptualModel.AssociationTypes.Any <AssociationType>((Func <AssociationType, bool>)(a => StoreGeneratedIdentityKeyConvention.IsNonTableSplittingForeignKey(a, property))) || StoreGeneratedIdentityKeyConvention.ParentOfTpc(item, model.ConceptualModel)))
            {
                return;
            }
            property.SetStoreGeneratedPattern(StoreGeneratedPattern.Identity);
        }
        protected virtual void ConfigureProperty(EdmProperty property)
        {
            if (IsNullable != null)
            {
                property.Nullable = IsNullable.Value;
            }

            if (ConcurrencyMode != null)
            {
                property.ConcurrencyMode = ConcurrencyMode.Value;
            }

            if (DatabaseGeneratedOption != null)
            {
                property.SetStoreGeneratedPattern((StoreGeneratedPattern)DatabaseGeneratedOption.Value);

                if (DatabaseGeneratedOption.Value
                    == ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
                {
                    property.Nullable = false;
                }
            }

            property.SetConfiguration(this);
        }
Exemplo n.º 3
0
        public void Can_generate_scalar_and_complex_properties_when_update()
        {
            var functionParameterMappingGenerator
                = new FunctionParameterMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest);

            var property0 = new EdmProperty("P0");
            var property1 = new EdmProperty("P1");
            var property2 = new EdmProperty("P2");

            property2.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed);
            property2.ConcurrencyMode = ConcurrencyMode.Fixed;

            var complexType = new ComplexType("CT", "N", DataSpace.CSpace);

            complexType.AddMember(property1);

            var complexProperty = EdmProperty.Complex("C", complexType);

            var parameterBindings
                = functionParameterMappingGenerator
                  .Generate(
                      ModificationOperator.Update,
                      new[] { property0, complexProperty, property2 },
                      new[]
            {
                new ColumnMappingBuilder(new EdmProperty("C0"), new[] { property0 }),
                new ColumnMappingBuilder(new EdmProperty("C_P1"), new[] { complexProperty, property1 }),
                new ColumnMappingBuilder(new EdmProperty("C2"), new[] { property2 })
            },
                      new List <EdmProperty>(),
                      useOriginalValues: true)
                  .ToList();

            Assert.Equal(3, parameterBindings.Count());

            var parameterBinding = parameterBindings.First();

            Assert.Equal("C0", parameterBinding.Parameter.Name);
            Assert.Same(property0, parameterBinding.MemberPath.Members.Single());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);

            parameterBinding = parameterBindings.ElementAt(1);

            Assert.Equal("C_P1", parameterBinding.Parameter.Name);
            Assert.Same(complexProperty, parameterBinding.MemberPath.Members.First());
            Assert.Same(property1, parameterBinding.MemberPath.Members.Last());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);

            parameterBinding = parameterBindings.Last();

            Assert.Equal("C2_Original", parameterBinding.Parameter.Name);
            Assert.Same(property2, parameterBinding.MemberPath.Members.Single());
            Assert.Equal("String", parameterBinding.Parameter.TypeName);
            Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode);
            Assert.False(parameterBinding.IsCurrent);
        }
Exemplo n.º 4
0
 protected virtual void ConfigureProperty(EdmProperty property)
 {
     if (this.IsNullable.HasValue)
     {
         property.Nullable = this.IsNullable.Value;
     }
     if (this.ConcurrencyMode.HasValue)
     {
         property.ConcurrencyMode = this.ConcurrencyMode.Value;
     }
     if (this.DatabaseGeneratedOption.HasValue)
     {
         property.SetStoreGeneratedPattern((StoreGeneratedPattern)this.DatabaseGeneratedOption.Value);
         if (this.DatabaseGeneratedOption.Value == System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
         {
             property.Nullable = false;
         }
     }
     property.SetConfiguration((object)this);
 }
Exemplo n.º 5
0
        internal virtual void Configure(EdmProperty property)
        {
            DebugCheck.NotNull(property);
            Debug.Assert(property.TypeUsage != null);

            var existingConfiguration = property.GetConfiguration() as PrimitivePropertyConfiguration;

            if (existingConfiguration != null)
            {
                string errorMessage;
                if ((existingConfiguration.OverridableConfigurationParts
                     & OverridableConfigurationParts.OverridableInCSpace)
                    != OverridableConfigurationParts.OverridableInCSpace &&
                    !existingConfiguration.IsCompatible(this, inCSpace: true, errorMessage: out errorMessage))
                {
                    var propertyInfo      = property.GetClrPropertyInfo();
                    var declaringTypeName = propertyInfo == null
                                                ? string.Empty
                                                : ObjectContextTypeCache.GetObjectType(propertyInfo.DeclaringType).
                                            FullNameWithNesting();
                    throw Error.ConflictingPropertyConfiguration(property.Name, declaringTypeName, errorMessage);
                }

                // Choose the more derived type for the merged configuration
                PrimitivePropertyConfiguration mergedConfiguration;
                if (existingConfiguration.GetType().IsAssignableFrom(GetType()))
                {
                    mergedConfiguration = (PrimitivePropertyConfiguration)MemberwiseClone();
                }
                else
                {
                    mergedConfiguration = (PrimitivePropertyConfiguration)existingConfiguration.MemberwiseClone();
                    mergedConfiguration.CopyFrom(this);
                }
                mergedConfiguration.FillFrom(existingConfiguration, inCSpace: true);
                property.SetConfiguration(mergedConfiguration);
            }
            else
            {
                property.SetConfiguration(this);
            }

            if (IsNullable != null)
            {
                property.Nullable = IsNullable.Value;
            }

            if (ConcurrencyMode != null)
            {
                property.ConcurrencyMode = ConcurrencyMode.Value;
            }

            if (DatabaseGeneratedOption != null)
            {
                property.SetStoreGeneratedPattern((StoreGeneratedPattern)DatabaseGeneratedOption.Value);

                if (DatabaseGeneratedOption.Value
                    == ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
                {
                    property.Nullable = false;
                }
            }
        }
Exemplo n.º 6
0
        private static bool UpdateColumnNamesForTableSharing(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            EntityType toTable,
            MappingFragment fragment)
        {
            List <EntityTypeMapping> mappingsUsingTable = EntityMappingConfiguration.FindAllTypeMappingsUsingTable(databaseMapping, toTable);
            Dictionary <EntityType, List <AssociationType> > dictionary = new Dictionary <EntityType, List <AssociationType> >();

            foreach (EntityTypeMapping entityTypeMapping in mappingsUsingTable)
            {
                EntityType entityType1 = entityTypeMapping.EntityType;
                if (entityType != entityType1)
                {
                    IEnumerable <AssociationType> associationTypes = EntityMappingConfiguration.FindAllOneToOneFKAssociationTypes(databaseMapping.Model, entityType, entityType1);
                    EntityType rootType = entityType1.GetRootType();
                    if (!dictionary.ContainsKey(rootType))
                    {
                        dictionary.Add(rootType, associationTypes.ToList <AssociationType>());
                    }
                    else
                    {
                        dictionary[rootType].AddRange(associationTypes);
                    }
                }
            }
            List <EntityType> source1 = new List <EntityType>();

            foreach (KeyValuePair <EntityType, List <AssociationType> > keyValuePair in dictionary)
            {
                if (keyValuePair.Key != entityType.GetRootType() && keyValuePair.Value.Count == 0)
                {
                    source1.Add(keyValuePair.Key);
                }
            }
            if (source1.Count > 0 && source1.Count == dictionary.Count)
            {
                DatabaseName tableName = toTable.GetTableName();
                throw Error.EntityMappingConfiguration_InvalidTableSharing((object)entityType.Name, (object)source1.First <EntityType>().Name, tableName != null ? (object)tableName.Name : (object)databaseMapping.Database.GetEntitySet(toTable).Table);
            }
            IEnumerable <AssociationType> source2 = dictionary.Values.SelectMany <List <AssociationType>, AssociationType>((Func <List <AssociationType>, IEnumerable <AssociationType> >)(l => (IEnumerable <AssociationType>)l));

            if (!source2.Any <AssociationType>())
            {
                return(false);
            }
            AssociationType associationType     = source2.First <AssociationType>();
            EntityType      entityType2         = associationType.Constraint.FromRole.GetEntityType();
            EntityType      dependentEntityType = entityType == entityType2?associationType.Constraint.ToRole.GetEntityType() : entityType;

            MappingFragment mappingFragment = entityType == entityType2?mappingsUsingTable.Single <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(etm => etm.EntityType == dependentEntityType)).Fragments.SingleOrDefault <MappingFragment>((Func <MappingFragment, bool>)(mf => mf.Table == toTable)) : fragment;

            if (mappingFragment != null)
            {
                List <EdmProperty> list1 = entityType2.KeyProperties().ToList <EdmProperty>();
                List <EdmProperty> list2 = dependentEntityType.KeyProperties().ToList <EdmProperty>();
                for (int index = 0; index < list1.Count; ++index)
                {
                    EdmProperty dependentKey = list2[index];
                    dependentKey.SetStoreGeneratedPattern(StoreGeneratedPattern.None);
                    mappingFragment.ColumnMappings.Single <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.PropertyPath.First <EdmProperty>() == dependentKey)).ColumnProperty.Name = list1[index].Name;
                }
            }
            return(true);
        }