public void Initialization_for_navigation_with_internal_constructor_throws()
        {
            var accessor = new ClrCollectionAccessorFactory().Create(CreateNavigation("AsMyInternalCollection"));

            Assert.Equal(
                CoreStrings.NavigationCannotCreateType("AsMyInternalCollection", typeof(MyEntity).Name, typeof(MyInternalCollection).Name),
                Assert.Throws <InvalidOperationException>(() => accessor.Add(new MyEntity(false), new MyOtherEntity())).Message);
        }
        public void Initialization_for_read_only_navigation_without_backing_field_throws()
        {
            var accessor = new ClrCollectionAccessorFactory().Create(CreateNavigation("ReadOnlyPropNoField"));

            Assert.Equal(
                CoreStrings.NavigationNoSetter("ReadOnlyPropNoField", typeof(MyEntity).Name),
                Assert.Throws <InvalidOperationException>(() => accessor.Add(new MyEntity(false), new MyOtherEntity())).Message);
        }
예제 #3
0
        public void Navigation_is_returned_if_it_implements_IClrCollectionAccessor()
        {
            var accessorMock   = new Mock <IClrCollectionAccessor>();
            var navigationMock = accessorMock.As <INavigation>();

            var source = new ClrCollectionAccessorFactory();

            Assert.Same(accessorMock.Object, source.Create(navigationMock.Object));
        }
        private void Enumerable_backed_by_non_collection_throws(Action <IClrCollectionAccessor, MyEntity, MyOtherEntity> test)
        {
            var accessor = new ClrCollectionAccessorFactory().Create(CreateNavigation("AsIEnumerableNotCollection"));

            var entity = new MyEntity(initialize: true);
            var value  = new MyOtherEntity();

            Assert.Equal(
                CoreStrings.NavigationBadType(
                    "AsIEnumerableNotCollection", typeof(MyEntity).Name, typeof(MyEnumerable).Name, typeof(MyOtherEntity).Name),
                Assert.Throws <InvalidOperationException>(() => test(accessor, entity, value)).Message);
        }
        private static void AccessorTest(
            string navigationName, Func <MyEntity, IEnumerable <MyOtherEntity> > reader, bool initializeCollections = true)
        {
            var accessor = new ClrCollectionAccessorFactory().Create(CreateNavigation(navigationName));

            var entity = new MyEntity(initializeCollections);

            var value = new MyOtherEntity();

            Assert.False(accessor.Contains(entity, value));
            accessor.Remove(entity, value);

            accessor.Add(entity, value);

            Assert.True(accessor.Contains(entity, value));
            Assert.Equal(1, reader(entity).Count());

            accessor.Remove(entity, value);

            Assert.False(accessor.Contains(entity, value));
            Assert.Equal(0, reader(entity).Count());
        }
예제 #6
0
        public void Delegate_accessor_always_creates_collections_that_use_reference_equality_comparer()
        {
            IMutableModel model      = new Model();
            var           entityType = model.AddEntityType(typeof(MyEntity));
            var           otherType  = model.AddEntityType(typeof(MyEntityWithCustomComparer));
            var           foreignKey = otherType.AddForeignKey(
                otherType.AddProperty("MyEntityId", typeof(int)),
                entityType.SetPrimaryKey(entityType.AddProperty("Id", typeof(int))),
                entityType);

            var navigation = foreignKey.HasPrincipalToDependent(
                typeof(MyEntity).GetProperty(
                    nameof(MyEntity.AsICollectionWithCustomComparer),
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));

            new BackingFieldConvention(new TestLogger <DbLoggerCategory.Model, TestLoggingDefinitions>())
            .Apply(((ForeignKey)foreignKey).Builder, (Navigation)navigation);

            var accessor = new ClrCollectionAccessorFactory().Create(navigation);

            var entity = new MyEntity(initialize: false);
            var value  = new MyEntityWithCustomComparer
            {
                Id = 1
            };

            Assert.False(accessor.Contains(entity, value));

            accessor.Add(entity, value);

            value.Id = 42;

            accessor.Add(entity, value);

            Assert.Equal(1, entity.AsICollectionWithCustomComparer.Count);
        }