Example #1
0
 public void Throws_if_create_proxy_when_proxies_not_enabled()
 {
     using var context = new NeweyContextN7();
     Assert.Equal(
         ProxiesStrings.ProxiesNotEnabled(nameof(RedBullRb3)),
         Assert.Throws <InvalidOperationException>(
             () => context.CreateProxy <RedBullRb3>()).Message);
 }
Example #2
0
    private static void CheckProxyOptions(IServiceProvider serviceProvider, string entityTypeName)
    {
        var options = serviceProvider.GetRequiredService <IDbContextOptions>().FindExtension <ProxiesOptionsExtension>();

        if (options?.UseProxies != true)
        {
            throw new InvalidOperationException(ProxiesStrings.ProxiesNotEnabled(entityTypeName));
        }
    }
        private static object CreateProxy(
            this IServiceProvider serviceProvider,
            Type entityType,
            params object[] constructorArguments)
        {
            var options = serviceProvider.GetService <IDbContextOptions>().FindExtension <ProxiesOptionsExtension>();

            if (options?.UseProxies != true)
            {
                throw new InvalidOperationException(ProxiesStrings.ProxiesNotEnabled(entityType.ShortDisplayName()));
            }

            return(serviceProvider.GetService <IProxyFactory>().Create(
                       serviceProvider.GetService <ICurrentDbContext>().Context,
                       entityType,
                       constructorArguments));
        }
        /// <summary>
        ///     Creates a proxy instance for an entity type if proxy creation has been turned on.
        /// </summary>
        /// <param name="context"> The <see cref="DbContext" />. </param>
        /// <param name="entityType"> The entity type for which a proxy is needed. </param>
        /// <param name="constructorArguments"> Arguments to pass to the entity type constructor. </param>
        /// <returns> The proxy instance. </returns>
        public static object CreateProxy(
            [NotNull] this DbContext context,
            [NotNull] Type entityType,
            [NotNull] params object[] constructorArguments)
        {
            Check.NotNull(context, nameof(context));
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(constructorArguments, nameof(constructorArguments));

            var options = context.GetService <IDbContextOptions>().FindExtension <ProxiesOptionsExtension>();

            if (options?.UseLazyLoadingProxies != true)
            {
                throw new InvalidOperationException(ProxiesStrings.ProxiesNotEnabled(entityType.ShortDisplayName()));
            }

            return(context.GetService <IProxyFactory>().Create(context, entityType, constructorArguments));
        }