예제 #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 WeakReference <object> TryGetEntity(
            ValueBuffer valueBuffer,
            bool throwOnNullKey,
            out bool hasNullKey)
        {
            var key = PrincipalKeyValueFactory.CreateFromBuffer(valueBuffer);

            if (key == null)
            {
                if (throwOnNullKey)
                {
                    if (Key.IsPrimaryKey())
                    {
                        throw new InvalidOperationException(
                                  CoreStrings.InvalidKeyValue(
                                      Key.DeclaringEntityType.DisplayName(),
                                      PrincipalKeyValueFactory.FindNullPropertyInValueBuffer(valueBuffer).Name));
                    }

                    throw new InvalidOperationException(
                              CoreStrings.InvalidAlternateKeyValue(
                                  Key.DeclaringEntityType.DisplayName(),
                                  PrincipalKeyValueFactory.FindNullPropertyInValueBuffer(valueBuffer).Name));
                }

                hasNullKey = true;
                return(null);
            }

            hasNullKey = false;
            return(_identityMap.TryGetValue((TKey)key, out var entity) ? entity : null);
        }
예제 #2
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(ValueBuffer valueBuffer, bool throwOnNullKey)
        {
            var key = PrincipalKeyValueFactory.CreateFromBuffer(valueBuffer);

            if (key == null &&
                throwOnNullKey)
            {
                if (Key.IsPrimaryKey())
                {
                    throw new InvalidOperationException(
                              CoreStrings.InvalidKeyValue(
                                  Key.DeclaringEntityType.DisplayName(),
                                  PrincipalKeyValueFactory.FindNullPropertyInValueBuffer(valueBuffer).Name));
                }

                throw new InvalidOperationException(
                          CoreStrings.InvalidAlternateKeyValue(
                              Key.DeclaringEntityType.DisplayName(),
                              PrincipalKeyValueFactory.FindNullPropertyInValueBuffer(valueBuffer).Name));
            }

            try
            {
                return(key != null &&
                       _identityMap.TryGetValue((TKey)key, out var entry)
                    ? entry
                    : null);
            }
            catch (InvalidCastException e)
            {
                throw new InvalidOperationException(
                          // ReSharper disable once PossibleNullReferenceException
                          CoreStrings.ErrorMaterializingPropertyInvalidCast(
                              Key.DeclaringEntityType.DisplayName(),
                              Key.Properties.First().Name,
                              typeof(TKey),
                              key.GetType()),
                          e);
            }
        }