Пример #1
0
        public void Throws_by_default_for_lazy_load_with_disposed_context()
        {
            var loggerFactory = new ListLoggerFactory();

            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddSingleton <ILoggerFactory>(loggerFactory)
                                  .BuildServiceProvider(validateScopes: true);

            using (var context = new WarningAsErrorContext(serviceProvider, defaultThrow: false))
            {
                context.Add(
                    new WarningAsErrorEntity {
                    Nav = new IncludedEntity()
                });
                context.SaveChanges();
            }

            WarningAsErrorEntity entity;

            using (var context = new WarningAsErrorContext(serviceProvider, defaultThrow: false))
            {
                entity = context.WarningAsErrorEntities.OrderBy(e => e.Id).First();
            }

            Assert.Equal(
                CoreStrings.WarningAsErrorTemplate(
                    CoreEventId.LazyLoadOnDisposedContextWarning.ToString(),
                    CoreResources.LogLazyLoadOnDisposedContext(new TestLogger <InMemoryLoggingDefinitions>())
                    .GenerateMessage("WarningAsErrorEntity", "Nav"),
                    "CoreEventId.LazyLoadOnDisposedContextWarning"),
                Assert.Throws <InvalidOperationException>(
                    () => entity.Nav).Message);
        }
Пример #2
0
        public void Throws_when_context_is_disposed()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddEntityFrameworkProxies()
                                  .AddDbContext <JammieDodgerContext>((p, b) =>
                                                                      b.UseInMemoryDatabase("Jammie")
                                                                      .UseInternalServiceProvider(p)
                                                                      .UseLazyLoadingProxies())
                                  .BuildServiceProvider();

            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetService <JammieDodgerContext>();
                context.Add(new Phone());
                context.SaveChanges();
            }

            Phone phone;

            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetService <JammieDodgerContext>();
                phone = context.Set <Phone>().Single();
            }

            Assert.Equal(
                CoreStrings.WarningAsErrorTemplate(
                    CoreEventId.LazyLoadOnDisposedContextWarning.ToString(),
                    CoreResources.LogLazyLoadOnDisposedContext(new TestLogger <TestLoggingDefinitions>()).GenerateMessage("Texts", "PhoneProxy"),
                    "CoreEventId.LazyLoadOnDisposedContextWarning"),
                Assert.Throws <InvalidOperationException>(
                    () => phone.Texts).Message);
        }
Пример #3
0
        public void Lazy_load_with_disposed_context_can_be_configured_to_log_at_debug_level()
        {
            var loggerFactory = new ListLoggerFactory();

            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddSingleton <ILoggerFactory>(loggerFactory)
                                  .BuildServiceProvider(validateScopes: true);

            using (var context = new WarningAsErrorContext(
                       serviceProvider,
                       defaultThrow: false,
                       toChangeLevel: (CoreEventId.LazyLoadOnDisposedContextWarning, LogLevel.Debug)))
            {
                context.Add(
                    new WarningAsErrorEntity {
                    Nav = new IncludedEntity()
                });
                context.SaveChanges();
            }

            WarningAsErrorEntity entity;

            using (var context = new WarningAsErrorContext(
                       serviceProvider,
                       defaultThrow: false,
                       toChangeLevel: (CoreEventId.LazyLoadOnDisposedContextWarning, LogLevel.Debug)))
            {
                entity = context.WarningAsErrorEntities.OrderBy(e => e.Id).First();
            }

            Assert.Null(entity.Nav);

            var log = loggerFactory.Log.Single(
                l => l.Message
                == CoreResources
                .LogLazyLoadOnDisposedContext(new TestLogger <InMemoryLoggingDefinitions>())
                .GenerateMessage("WarningAsErrorEntity", "Nav"));

            Assert.Equal(LogLevel.Debug, log.Level);
        }