示例#1
0
        public void Can_use_PropertyAccessorsFactory_on_non_indexed_property()
        {
            IMutableModel model      = new Model();
            var           entityType = model.AddEntityType(typeof(NonIndexedClass));

            entityType.AddProperty("Id", typeof(int));
            var propA = entityType.AddProperty("PropA", typeof(string));

            model.FinalizeModel();

            var contextServices = InMemoryTestHelpers.Instance.CreateContextServices(model);
            var stateManager    = contextServices.GetRequiredService <IStateManager>();
            var factory         = contextServices.GetRequiredService <IInternalEntityEntryFactory>();

            var entity = new NonIndexedClass();
            var entry  = factory.Create(stateManager, entityType, entity);

            var propertyAccessors = new PropertyAccessorsFactory().Create(propA);

            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.CurrentValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.OriginalValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.PreStoreGeneratedCurrentValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.RelationshipSnapshotGetter)(entry));

            var valueBuffer = new ValueBuffer(new object[] { 1, "ValueA" });

            Assert.Equal("ValueA", propertyAccessors.ValueBufferGetter(valueBuffer));
        }
        public void Can_use_PropertyAccessorsFactory_on_non_indexed_property()
        {
            var modelBuilder      = InMemoryTestHelpers.Instance.CreateConventionBuilder();
            var entityTypeBuilder = modelBuilder.Entity <NonIndexedClass>();

            entityTypeBuilder.Property <int>("Id");
            var propA = entityTypeBuilder.Property <string>("PropA").Metadata;

            var model = modelBuilder.FinalizeModel();

            var contextServices = InMemoryTestHelpers.Instance.CreateContextServices(model);
            var stateManager    = contextServices.GetRequiredService <IStateManager>();

            var entity = new NonIndexedClass();
            var entry  = new InternalEntityEntry(stateManager, (IEntityType)entityTypeBuilder.Metadata, entity);

            var propertyAccessors = new PropertyAccessorsFactory().Create((IProperty)propA);

            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.CurrentValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.OriginalValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.PreStoreGeneratedCurrentValueGetter)(entry));
            Assert.Equal("ValueA", ((Func <InternalEntityEntry, string>)propertyAccessors.RelationshipSnapshotGetter)(entry));

            var valueBuffer = new ValueBuffer(new object[] { 1, "ValueA" });

            Assert.Equal("ValueA", propertyAccessors.ValueBufferGetter(valueBuffer));
        }
        public void Exception_is_returned_for_indexed_property_without_indexer()
        {
            var entityType = new Model().AddEntityType(typeof(NonIndexedClass));
            var idProperty = entityType.AddProperty("Id", typeof(int));
            var propertyA  = entityType.AddIndexedProperty("PropertyA", typeof(string));
            var propertyB  = entityType.AddIndexedProperty("PropertyB", typeof(int));

            var nonIndexedClass = new NonIndexedClass();

            Assert.Throws <InvalidOperationException>(
                () => new IndexedPropertyGetterFactory().Create(propertyA).GetClrValue(nonIndexedClass));
            Assert.Throws <InvalidOperationException>(
                () => new IndexedPropertyGetterFactory().Create(propertyB).GetClrValue(nonIndexedClass));
        }
        public void Exception_is_returned_when_setting_indexed_property_without_indexer()
        {
            var entityType = new Model().AddEntityType(typeof(NonIndexedClass));
            var idProperty = entityType.AddProperty("Id", typeof(int));
            var propertyA  = entityType.AddIndexedProperty("PropertyA", typeof(string));
            var propertyB  = entityType.AddIndexedProperty("PropertyB", typeof(int));

            var indexedClass = new NonIndexedClass
            {
                Id    = 1,
                PropA = "PropAValue",
                PropB = 123
            };

            Assert.Throws <InvalidOperationException>(
                () => new IndexedPropertySetterFactory().Create(propertyA).SetClrValue(indexedClass, "UpdatedValueA"));
            Assert.Throws <InvalidOperationException>(
                () => new IndexedPropertySetterFactory().Create(propertyB).SetClrValue(indexedClass, 456));
        }