Example #1
0
        public void ContainsExactEntity()
        {
            var entityCollection = new DbEntityCollection <EntityCollectionModel>();
            var entity           = new EntityCollectionModel
            {
                Id = "ABC"
            };

            entityCollection.Add(entity);

            Assert.IsTrue(entityCollection.Contains(entity));
        }
Example #2
0
        public void ContainsEntityById()
        {
            var entityCollection = new DbEntityCollection <EntityCollectionModel>();
            var entity           = new EntityCollectionModel
            {
                Id    = "ABC",
                Title = "1"
            };

            entityCollection.Add(entity);

            var idMatchingEntity = new EntityCollectionModel
            {
                Id = "ABC"
            };

            Assert.IsTrue(entityCollection.Contains(idMatchingEntity));
        }
#pragma warning restore CRR0026 // Unused member - used via Reflection

        private IDbEntityCollection <TRelatedEntity> BuildRelatedEntityCollection <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities)
        {
            var collection   = new DbEntityCollection <TRelatedEntity>();
            var entityMapper = new EntityMapper <TRelatedEntity>();

            var defaultId = entityMapper.GetDefaultId();
            var relatedEntitiesToProcess = new List <TRelatedEntity>();

            foreach (var entity in entities)
            {
                if (relationship.IsCollection)
                {
                    if (relationship.NavigationProperty.GetValue(entity) is ICollection <TRelatedEntity> navigationCollection)
                    {
                        relatedEntitiesToProcess.AddRange(navigationCollection);
                    }
                }
                else
                {
                    var relatedEntity = (TRelatedEntity)relationship.NavigationProperty.GetValue(entity);
                    if (relatedEntity != null)
                    {
                        relatedEntitiesToProcess.Add(relatedEntity);
                    }
                }
            }

            foreach (var relatedEntity in relatedEntitiesToProcess)
            {
                var entityId = entityMapper.GetIdValue(relatedEntity);
                if (Equals(entityId, defaultId))
                {
                    collection.Add(relatedEntity);
                }
            }

            return(collection);
        }