Exemple #1
0
        public void Configure_should_configure_and_order_keys_when_keys_and_order_specified()
        {
            var entityType = new EntityType
            {
                Name = "E"
            };
            var property = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);
            var property1 = EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo2       = new MockPropertyInfo(typeof(int), "P2");

            entityTypeConfiguration.Key(mockPropertyInfo2);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;
            (entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P2")).SetClrPropertyInfo(mockPropertyInfo2);
            var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");

            entityTypeConfiguration.Key(mockPropertyInfo1);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;
            (entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1")).SetClrPropertyInfo(mockPropertyInfo1);

            entityTypeConfiguration.Configure(entityType, new EdmModel());

            Assert.Equal(2, entityType.DeclaredKeyProperties.Count);
            Assert.Equal("P1", entityType.DeclaredKeyProperties.First().Name);
        }
Exemple #2
0
 /// <summary>
 /// Configures this property to be part of the entity type's primary key.
 /// </summary>
 /// <returns>
 /// The same <see cref="T:System.Data.Entity.ModelConfiguration.Configuration.ConventionPrimitivePropertyConfiguration" /> instance so that
 /// multiple calls can be chained.
 /// </returns>
 public virtual ConventionPrimitivePropertyConfiguration IsKey()
 {
     if (this._configuration() != null)
     {
         EntityTypeConfiguration typeConfiguration = this._configuration().TypeConfiguration as EntityTypeConfiguration;
         if (typeConfiguration != null && !typeConfiguration.IsKeyConfigured)
         {
             typeConfiguration.Key(this.ClrPropertyInfo);
         }
     }
     return(this);
 }
        public void Cloning_an_entity_configuration_clones_its_key_information()
        {
            var configuration = new EntityTypeConfiguration(typeof(object));

            var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");

            configuration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;

            var mockPropertyInfo2 = new MockPropertyInfo(typeof(int), "P2");

            configuration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;

            // This will set _isKeyConfigured to true
            configuration.Key(
                new List <PropertyInfo>
            {
                mockPropertyInfo1
            });

            var clone = configuration.Clone();

            VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);

            // This should have no effect because _isKeyConfigured is set to true
            clone.Key(mockPropertyInfo2, null);

            VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);

            // This should change the key on the original, but not on the clone.
            configuration.Key(
                new List <PropertyInfo>
            {
                mockPropertyInfo2
            });

            VerifyKeyProperty(configuration, "P2", mockPropertyInfo1, mockPropertyInfo2);
            VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);
        }
        public void HasKey_is_noop_when_set()
        {
            var type        = typeof(AType);
            var innerConfig = new EntityTypeConfiguration(type);

            innerConfig.Key(new[] { type.GetDeclaredProperty("Property1") });
            var config = new ConventionTypeConfiguration(type, () => innerConfig, new ModelConfiguration());

            var result = config.HasKey("Property2");

            Assert.Equal(1, innerConfig.KeyProperties.Count());
            Assert.False(innerConfig.KeyProperties.Any(p => p.Name == "Property2"));
            Assert.Same(config, result);
        }
Exemple #5
0
        public void Configure_should_throw_when_key_property_not_found()
        {
            var entityType = new EntityType
            {
                Name = "E"
            };
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo        = new MockPropertyInfo(typeof(int), "Id");

            entityTypeConfiguration.Key(mockPropertyInfo);

            Assert.Equal(
                Strings.KeyPropertyNotFound(("Id"), "E"),
                Assert.Throws <InvalidOperationException>(() => entityTypeConfiguration.Configure(entityType, new EdmModel())).Message);
        }
Exemple #6
0
        internal override void ApplyPropertyTypeConfiguration <TStructuralTypeConfiguration>(
            PropertyInfo propertyInfo,
            Func <TStructuralTypeConfiguration> structuralTypeConfiguration,
            System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration modelConfiguration)
        {
            if (!(typeof(TStructuralTypeConfiguration) == typeof(EntityTypeConfiguration)) || !this._attributeProvider.GetAttributes(propertyInfo).OfType <KeyAttribute>().Any <KeyAttribute>())
            {
                return;
            }
            EntityTypeConfiguration typeConfiguration = (EntityTypeConfiguration)(object)structuralTypeConfiguration();

            if (!propertyInfo.IsValidEdmScalarProperty())
            {
                return;
            }
            typeConfiguration.Key(propertyInfo);
        }
        public void IsKey_is_noop_when_set()
        {
            var typeConfig = new EntityTypeConfiguration(typeof(AType2));

            typeConfig.Key(new[] { typeof(AType2).GetDeclaredProperty("Property1") });
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
            {
                TypeConfiguration = typeConfig
            };
            var propertyInfo = typeof(AType2).GetDeclaredProperty("Property2");
            var config       = new ConventionPrimitivePropertyConfiguration(propertyInfo, () => innerConfig);

            var result = config.IsKey();

            Assert.DoesNotContain(propertyInfo, typeConfig.KeyProperties);
            Assert.Same(config, result);
        }
Exemple #8
0
        public void HasKey_composite_is_noop_when_set()
        {
            var type = new MockType()
                       .Property <int>("Property1")
                       .Property <int>("Property2")
                       .Property <int>("Property3");
            var innerConfig = new EntityTypeConfiguration(type);

            innerConfig.Key(new[] { type.GetProperty("Property1") });
            var config = new ConventionTypeConfiguration(type, () => innerConfig, new ModelConfiguration());

            var result = config.HasKey(new[] { "Property2", "Property3" });

            Assert.Equal(1, innerConfig.KeyProperties.Count());
            Assert.False(innerConfig.KeyProperties.Any(p => p.Name == "Property2"));
            Assert.False(innerConfig.KeyProperties.Any(p => p.Name == "Property3"));
            Assert.Same(config, result);
        }
Exemple #9
0
        public void Configure_should_throw_when_key_properties_and_not_root_type()
        {
            var entityType = new EntityType
            {
                Name     = "E",
                BaseType = new EntityType()
            };
            var type          = typeof(string);
            var tempQualifier = entityType.BaseType;

            tempQualifier.Annotations.SetClrType(type);
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));

            entityTypeConfiguration.Key(new MockPropertyInfo(typeof(int), "Id"));

            Assert.Equal(
                Strings.KeyRegisteredOnDerivedType(typeof(object), typeof(string)),
                Assert.Throws <InvalidOperationException>(() => entityTypeConfiguration.Configure(entityType, new EdmModel())).Message);
        }
Exemple #10
0
        public void IsKey_is_noop_when_set()
        {
            var type = new MockType()
                       .Property <int>("Property1")
                       .Property <int>("Property2");
            var typeConfig = new EntityTypeConfiguration(type);

            typeConfig.Key(new[] { type.GetProperty("Property1") });
            var innerConfig = new PrimitivePropertyConfiguration
            {
                TypeConfiguration = typeConfig
            };
            var propertyInfo = type.GetProperty("Property2");
            var config       = new LightweightPropertyConfiguration(propertyInfo, () => innerConfig);

            var result = config.IsKey();

            Assert.DoesNotContain(propertyInfo, typeConfig.KeyProperties);
            Assert.Same(config, result);
        }
        public void Configure_should_throw_when_key_property_not_found()
        {
            var entityType = new EntityType
                                 {
                                     Name = "E"
                                 };
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo = new MockPropertyInfo(typeof(int), "Id");
            entityTypeConfiguration.Key(mockPropertyInfo);

            Assert.Equal(
                Strings.KeyPropertyNotFound(("Id"), "E"),
                Assert.Throws<InvalidOperationException>(() => entityTypeConfiguration.Configure(entityType, new EdmModel())).Message);
        }
        public void Configure_should_throw_when_key_properties_and_not_root_type()
        {
            var entityType = new EntityType
                                 {
                                     Name = "E",
                                     BaseType = new EntityType()
                                 };
            var type = typeof(string);
            var tempQualifier = entityType.BaseType;

            tempQualifier.Annotations.SetClrType(type);
            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            entityTypeConfiguration.Key(new MockPropertyInfo(typeof(int), "Id"));

            Assert.Equal(
                Strings.KeyRegisteredOnDerivedType(typeof(object), typeof(string)),
                Assert.Throws<InvalidOperationException>(() => entityTypeConfiguration.Configure(entityType, new EdmModel())).Message);
        }
        public void Configure_should_configure_and_order_keys_when_keys_and_order_specified()
        {
            var entityType = new EntityType
                                 {
                                     Name = "E"
                                 };
            var property = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);
            var property1 = EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo2 = new MockPropertyInfo(typeof(int), "P2");
            entityTypeConfiguration.Key(mockPropertyInfo2);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;
            (entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P2")).SetClrPropertyInfo(mockPropertyInfo2);
            var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");
            entityTypeConfiguration.Key(mockPropertyInfo1);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;
            (entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "P1")).SetClrPropertyInfo(mockPropertyInfo1);

            entityTypeConfiguration.Configure(entityType, new EdmModel());

            Assert.Equal(2, entityType.DeclaredKeyProperties.Count);
            Assert.Equal("P1", entityType.DeclaredKeyProperties.First().Name);
        }
        public void Configure_should_configure_and_order_keys_when_keys_and_order_specified()
        {
            var entityType = new EdmEntityType { Name = "E" };
            entityType.AddPrimitiveProperty("P2").PropertyType.EdmType = EdmPrimitiveType.Int32;
            entityType.AddPrimitiveProperty("P1").PropertyType.EdmType = EdmPrimitiveType.Int32;

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));
            var mockPropertyInfo2 = new MockPropertyInfo(typeof(int), "P2");
            entityTypeConfiguration.Key(mockPropertyInfo2);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;
            entityType.GetDeclaredPrimitiveProperty("P2").SetClrPropertyInfo(mockPropertyInfo2);
            var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");
            entityTypeConfiguration.Key(mockPropertyInfo1);
            entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;
            entityType.GetDeclaredPrimitiveProperty("P1").SetClrPropertyInfo(mockPropertyInfo1);

            entityTypeConfiguration.Configure(entityType, new EdmModel());

            Assert.Equal(2, entityType.DeclaredKeyProperties.Count);
            Assert.Equal("P1", entityType.DeclaredKeyProperties.First().Name);
        }