Exemplo n.º 1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual InternalEntityEntry TryGetEntry(object entity)
        {
            if (_entityReferenceMap.TryGetValue(entity, out InternalEntityEntry entry))
            {
                return(entry);
            }

            var type  = entity.GetType();
            var found = false;

            foreach (var keyValue in _dietReferenceMap)
            {
                // ReSharper disable once CheckForReferenceEqualityInstead.2
                if (Equals(keyValue.Key.ClrType, type) &&
                    keyValue.Value.TryGetValue(entity, out entry))
                {
                    if (found)
                    {
                        throw new InvalidOperationException(CoreStrings.AmbiguousDelegatedIdentityEntity(
                                                                entity.GetType().ShortDisplayName(),
                                                                "." + nameof(EntityEntry.Reference) + "()." + nameof(ReferenceEntry.TargetEntry)));
                    }
                    found = true;
                }
            }

            return(entry);
        }
Exemplo n.º 2
0
        public void Can_get_owned_entity_entry()
        {
            using (var context = new FixupContext())
            {
                var principal = new ParentPN {
                    Id = 77
                };
                var dependent = new ChildPN {
                    Name = "1"
                };
                principal.Child1 = dependent;
                principal.Child2 = dependent;

                Assert.Equal(CoreStrings.UntrackedDelegatedIdentityEntity(
                                 typeof(ChildPN).ShortDisplayName(),
                                 "." + nameof(EntityEntry.Reference) + "()." + nameof(ReferenceEntry.TargetEntry)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry(dependent)).Message);

                var dependentEntry1 = context.Entry(principal).Reference(p => p.Child1).TargetEntry;

                Assert.Same(dependentEntry1.GetInfrastructure(), context.Entry(dependent).GetInfrastructure());

                var dependentEntry2 = context.Entry(principal).Reference(p => p.Child2).TargetEntry;

                Assert.Equal(CoreStrings.AmbiguousDelegatedIdentityEntity(
                                 typeof(ChildPN).ShortDisplayName(),
                                 "." + nameof(EntityEntry.Reference) + "()." + nameof(ReferenceEntry.TargetEntry)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry(dependent)).Message);
            }
        }