public void Delegate_getter_is_returned_for_IProperty_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty("Id", typeof(int), shadow: false);

            Assert.Equal(7, new ClrPropertyGetterFactory().Create(idProperty).GetClrValue(new Customer { Id = 7 }));
        }
Exemplo n.º 2
0
        public void Can_set_ClrType()
        {
            var entityType = new Model().AddEntityType(typeof(object));
            var property = entityType.AddProperty("Kake", typeof(string));

            Assert.Equal(typeof(string), property.ClrType);
        }
Exemplo n.º 3
0
        public void Change_tracking_strategy_can_be_changed()
        {
            var model = new Model { ChangeTrackingStrategy = ChangeTrackingStrategy.ChangingAndChangedNotifications };
            Assert.Equal(ChangeTrackingStrategy.ChangingAndChangedNotifications, model.ChangeTrackingStrategy);

            model.ChangeTrackingStrategy = ChangeTrackingStrategy.ChangedNotifications;
            Assert.Equal(ChangeTrackingStrategy.ChangedNotifications, model.GetChangeTrackingStrategy());
        }
        public void Model_can_be_set_explicitly_in_options()
        {
            var model = new Model();

            var optionsBuilder = new DbContextOptionsBuilder().UseModel(model);

            Assert.Same(model, optionsBuilder.Options.FindExtension<CoreOptionsExtension>().Model);
        }
Exemplo n.º 5
0
        public void Can_get_model()
        {
            var model = new Model();

            var sequence = Sequence.GetOrAddSequence(model, RelationalFullAnnotationNames.Instance.SequencePrefix, "Foo");

            Assert.Same(model, sequence.Model);
        }
Exemplo n.º 6
0
        public void Constructor_validates_properties_from_same_entity()
        {
            var property1 = new Model().AddEntityType(typeof(Customer)).GetOrAddProperty(Customer.IdProperty);
            var property2 = new Model().AddEntityType(typeof(Order)).GetOrAddProperty(Order.IdProperty);

            Assert.Equal(CoreStrings.IndexPropertiesWrongEntity($"{{'{property1.Name}', '{property2.Name}'}}", typeof(Customer).Name),
                Assert.Throws<InvalidOperationException>(
                    () => property1.DeclaringEntityType.AddIndex(new[] { property1, property2 })).Message);
        }
Exemplo n.º 7
0
 private ForeignKey CreateForeignKey()
 {
     var model = new Model();
     var entityType = model.AddEntityType(typeof(E));
     var idProperty = entityType.AddProperty("id", typeof(int));
     var key = entityType.SetPrimaryKey(idProperty);
     var fkProperty = entityType.AddProperty("p", typeof(int));
     return entityType.AddForeignKey(fkProperty, key, entityType);
 }
Exemplo n.º 8
0
        public void M_underscore_matching_field_is_not_used_if_type_is_not_compatible()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSideOfTheMoon));
            var property = entityType.AddProperty("SpeakToMe", typeof(int), shadow: false);
            var propertyInfo = entityType.ClrType.GetAnyProperty("SpeakToMe");
            var fields = propertyInfo.DeclaringType.GetRuntimeFields().ToDictionary(f => f.Name);

            Assert.Null(new FieldMatcher().TryMatchFieldName(property, propertyInfo, fields));
        }
        public void Sensitive_data_logging_can_be_set_explicitly_in_options()
        {
            var model = new Model();

            var optionsBuilder = new DbContextOptionsBuilder().UseModel(model).EnableSensitiveDataLogging();

            Assert.Same(model, optionsBuilder.Options.FindExtension<CoreOptionsExtension>().Model);
            Assert.True(optionsBuilder.Options.FindExtension<CoreOptionsExtension>().IsSensitiveDataLoggingEnabled);
        }
Exemplo n.º 10
0
        public void Throws_if_no_match_found_and_no_property_setter()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.TimeProperty);

            Assert.Equal(
                CoreStrings.NoFieldOrSetter(typeof(TheDarkSide).FullName, "Time"),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
        private static Model CreateModel(bool generateValues = true)
        {
            var model = new Model();

            var entityType = model.AddEntityType("Led");
            entityType.AddProperty("Zeppelin", typeof(Guid));
            entityType.AddProperty("Stairway", typeof(Guid)).RequiresValueGenerator = generateValues;

            return model;
        }
        public virtual void Does_not_detect_duplicate_table_names_for_inherited_entities()
        {
            var model = new Model();
            var entityA = model.AddEntityType(typeof(A));
            SetPrimaryKey(entityA);
            var entityC = model.AddEntityType(typeof(C));
            SetBaseType(entityC, entityA);

            Validate(model);
        }
Exemplo n.º 13
0
        public void Field_name_match_is_used_in_preference_to_property_setter()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.BreatheProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<FieldInfo>(mapping.Item2);
            Assert.Equal("_breathe", mapping.Item2.Name);
        }
Exemplo n.º 14
0
        private static void FieldMatchTest(string propertyName, string fieldName)
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSideOfTheMoon));
            var property = entityType.AddProperty(propertyName, typeof(int), shadow: false);
            var propertyInfo = entityType.ClrType.GetAnyProperty(propertyName);
            var fields = propertyInfo.DeclaringType.GetRuntimeFields().ToDictionary(f => f.Name);

            var matchedField = new FieldMatcher().TryMatchFieldName(property, propertyInfo, fields);

            Assert.Equal(fieldName, matchedField.Name);
        }
Exemplo n.º 15
0
        public void Default_nullability_of_property_is_based_on_nullability_of_CLR_type()
        {
            var entityType = new Model().AddEntityType(typeof(object));
            var stringProperty = entityType.AddProperty("stringName", typeof(string));
            var nullableIntProperty = entityType.AddProperty("nullableIntName", typeof(int?));
            var intProperty = entityType.AddProperty("intName", typeof(int));

            Assert.True(stringProperty.IsNullable);
            Assert.True(nullableIntProperty.IsNullable);
            Assert.False(intProperty.IsNullable);
        }
        public void Property_added_by_name_is_non_shadow_if_matches_Clr_property()
        {
            var model = new Model();
            var modelBuilder = new InternalModelBuilder(model);
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);
            var property = builder.Metadata;

            Assert.Equal(typeof(string), property.ClrType);
            Assert.False(property.IsShadowProperty);
        }
Exemplo n.º 17
0
        public void Throws_if_annotated_field_if_types_not_compatible()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "_badFieldForSpeak";

            Assert.Equal(
                CoreStrings.BadBackingFieldType("_badFieldForSpeak", typeof(string).Name, typeof(TheDarkSide).FullName, "SpeakToMe", typeof(int).Name),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
Exemplo n.º 18
0
        public void Throws_if_annotated_field_name_is_not_found()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "_speakToMe";

            Assert.Equal(
                CoreStrings.MissingBackingField(typeof(TheDarkSide).FullName, "SpeakToMe", "_speakToMe"),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
Exemplo n.º 19
0
        public void Validates_properties_from_same_entity()
        {
            var entityType1 = new Model().AddEntityType(typeof(Customer));
            var entityType2 = new Model().AddEntityType(typeof(Order));
            var property1 = entityType1.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType2.GetOrAddProperty(Order.NameProperty);

            Assert.Equal(CoreStrings.KeyPropertiesWrongEntity($"{{'{property1.Name}', '{property2.Name}'}}", entityType1.DisplayName()),
                Assert.Throws<InvalidOperationException>(
                    () => entityType1.AddKey(new[] { property1, property2 })).Message);
        }
Exemplo n.º 20
0
        public void Property_setter_is_used_if_no_matching_field_is_found()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.OnTheRunProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<PropertyInfo>(mapping.Item2);
            Assert.Equal("OnTheRun", mapping.Item2.Name);
        }
Exemplo n.º 21
0
        public void Annotated_field_name_is_used_if_present()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "fieldForSpeak";

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.Equal("fieldForSpeak", mapping.Item2.Name);
        }
        public void Delegate_setter_can_set_reference_type_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.ContentProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterFactory().Create(idProperty).SetClrValue(customer, "MyString");

            Assert.Equal("MyString", customer.Content);
        }
        public void Delegate_setter_can_set_nullable_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.OptionalIntProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterFactory().Create(idProperty).SetClrValue(customer, 3);

            Assert.Equal(3, customer.OptionalInt);
        }
        public void Delegate_setter_can_set_value_type_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.IdProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterFactory().Create(idProperty).SetClrValue(customer, 1);

            Assert.Equal(1, customer.Id);
        }
        public void Delegate_setter_is_returned_for_IProperty_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.IdProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterFactory().Create(idProperty).SetClrValue(customer, 77);

            Assert.Equal(77, customer.Id);
        }
        public void Entity_returns_same_instance_for_entity_type_name()
        {
            var model = new Model();
            var modelBuilder = CreateModelBuilder(model);

            var entityBuilder = modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.DataAnnotation);

            Assert.NotNull(entityBuilder);
            Assert.NotNull(model.FindEntityType(typeof(Customer).FullName));
            Assert.Same(entityBuilder, modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit));
        }
Exemplo n.º 27
0
        public void Can_create_key_from_properties()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var property1 = entityType.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType.GetOrAddProperty(Customer.NameProperty);
            property2.IsNullable = false;

            var key = entityType.AddKey(new[] { property1, property2 }, ConfigurationSource.Convention);

            Assert.True(new[] { property1, property2 }.SequenceEqual(key.Properties));
            Assert.Equal(ConfigurationSource.Convention, key.GetConfigurationSource());
        }
Exemplo n.º 28
0
        public void Can_create_index_from_properties()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var property1 = entityType.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType.GetOrAddProperty(Customer.NameProperty);

            var index = entityType.AddIndex(new[] { property1, property2 }, ConfigurationSource.Convention);

            Assert.True(new[] { property1, property2 }.SequenceEqual(index.Properties));
            Assert.False(index.IsUnique);
            Assert.Equal(ConfigurationSource.Convention, index.GetConfigurationSource());
        }
        public void It_adds_provider_method_names()
        {
            var model = new Model();

            Assert.Null(model.Scaffolding().UseProviderMethodName);

            model.Scaffolding().UseProviderMethodName = "UsePutRelationalProviderNameHere";
            Assert.Equal("UsePutRelationalProviderNameHere", model.Scaffolding().UseProviderMethodName);

            model.Scaffolding().UseProviderMethodName = null;
            Assert.Null(model.Scaffolding().UseProviderMethodName);
        }
Exemplo n.º 30
0
        public void Can_create_unique_index_from_properties()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var property1 = entityType.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType.GetOrAddProperty(Customer.NameProperty);

            var index = entityType.AddIndex(new[] { property1, property2 });
            index.IsUnique = true;

            Assert.True(new[] { property1, property2 }.SequenceEqual(index.Properties));
            Assert.True(index.IsUnique);
        }
        public void OnModelCreating_VerifyModelCreation()
        {
            var mockModel = new Mock <ModelBuilder>(new ConventionSet());

            try
            {
                var contextOptions = new DbContextOptions <ProjectManagerDbContext>();
                var projectManagerDbContextStub = new ProjectManagerDbContextStub(contextOptions);
                var modelBuilder = new ModelBuilder(new ConventionSet());
                var model        = new Microsoft.EntityFrameworkCore.Metadata.Internal.Model();
                var configSource = new ConfigurationSource();

                var internalModelBuilder = new InternalModelBuilder(model);

                var taskModel = new TaskDetail();
                var entity    = new EntityType("TaskModel", model, configSource);
                var internalEntityTypeBuilder = new InternalEntityTypeBuilder(entity, internalModelBuilder);
                var entityTypeBuilder         = new EntityTypeBuilder <TaskDetail>(internalEntityTypeBuilder);
                mockModel.Setup(m => m.Entity <TaskDetail>()).Returns(entityTypeBuilder);
                var property = new Property("Name", taskModel.GetType(), taskModel.GetType().GetProperty("Name"), taskModel.GetType().GetField("Name"), entity, configSource, null);
                var internalPropertyBuilder = new InternalPropertyBuilder(property, internalModelBuilder);
                var propertyBuilder         = new PropertyBuilder <string>(internalPropertyBuilder);

                var userModel  = new User();
                var userEntity = new EntityType("User", model, configSource);
                var userInternalEntityTypeBuilder = new InternalEntityTypeBuilder(userEntity, internalModelBuilder);
                var userEntityTypeBuilder         = new EntityTypeBuilder <User>(userInternalEntityTypeBuilder);
                mockModel.Setup(m => m.Entity <User>()).Returns(userEntityTypeBuilder);
                var userProperty = new Property("FirstName", userModel.GetType(), userModel.GetType().GetProperty("FirstName"), userModel.GetType().GetField("FirstName"), entity, configSource, null);
                var userInternalPropertyBuilder = new InternalPropertyBuilder(userProperty, internalModelBuilder);
                var userPropertyBuilder         = new PropertyBuilder <string>(userInternalPropertyBuilder);

                var projectModel  = new Project();
                var projectEntity = new EntityType("Project", model, configSource);
                var projectInternalEntityTypeBuilder = new InternalEntityTypeBuilder(projectEntity, internalModelBuilder);
                var projectEntityTypeBuilder         = new EntityTypeBuilder <Project>(projectInternalEntityTypeBuilder);
                mockModel.Setup(m => m.Entity <Project>()).Returns(projectEntityTypeBuilder);
                var projectProperty = new Property("FirstName", projectModel.GetType(), projectModel.GetType().GetProperty("FirstName"), projectModel.GetType().GetField("FirstName"), entity, configSource, null);
                var projectInternalPropertyBuilder = new InternalPropertyBuilder(projectProperty, internalModelBuilder);
                var projectPropertyBuilder         = new PropertyBuilder <string>(projectInternalPropertyBuilder);

                projectManagerDbContextStub.TestModelCreation(modelBuilder);
            }
            catch (Exception ex)
            {
                mockModel.Verify(m => m.Entity <TaskDetail>().HasKey("Id"), Times.Once);
                mockModel.Verify(m => m.Entity <User>().HasKey("UserId"), Times.Once);
                mockModel.Verify(m => m.Entity <Project>().HasKey("ProjectId"), Times.Once);
                Assert.NotNull(ex);
            }
        }
Exemplo n.º 32
0
 public int Insert(string table, Microsoft.EntityFrameworkCore.Metadata.Internal.Model model)
 {
     //InsertCommand command = new InsertCommand(BaseConnection, table, model.GetColumns(), ToSQLType.Convert(model.GetValues()));
     //return command.Execute();
     return(0);
 }