public void Direct_use_of_Set_throws_if_context_disposed()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            Assert.Throws <ObjectDisposedException>(() => context.Set <Category>());
        }
        public void It_throws_with_derived_name()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            var ex = Assert.Throws <ObjectDisposedException>(() => context.Model);
        }
Example #3
0
        public void Direct_use_of_Query_throws_if_context_disposed()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            Assert.Throws <ObjectDisposedException>(() => context.Query <Curious>());
        }
Example #4
0
        public void Direct_use_of_Set_for_shared_type_throws_if_context_disposed()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            Assert.Throws <ObjectDisposedException>(() => context.Set <Dictionary <string, object> >("SharedTypeEntityTypeName"));
        }
Example #5
0
        public void Direct_use_of_Set_throws_if_context_disposed()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => context.Set <Category>()).Message);
        }
Example #6
0
        public async Task Use_of_query_throws_if_obtained_from_disposed_context()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            var query = context.Georges;

            Assert.Throws <ObjectDisposedException>(() => query.ToList());
            await Assert.ThrowsAsync <ObjectDisposedException>(() => query.ToListAsync());
        }
Example #7
0
        public async Task Use_of_set_throws_if_obtained_from_disposed_context()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            var set = context.Categories;

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.Add(new Category())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.Find(77)).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.Attach(new Category())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.Update(new Category())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.Remove(new Category())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                Assert.Throws <ObjectDisposedException>(() => set.ToList()).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => set.AddAsync(new Category()).AsTask())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => set.FindAsync(77).AsTask())).Message);

            Assert.StartsWith(
                CoreStrings.ContextDisposed,
                (await Assert.ThrowsAsync <ObjectDisposedException>(() => set.ToListAsync())).Message);
        }
        public void It_throws_with_derived_name()
        {
            var context = new EarlyLearningCenter();

            context.Dispose();

            var ex = Assert.Throws<ObjectDisposedException>(() => context.Model);
            Assert.Contains(nameof(EarlyLearningCenter), ex.Message);
        }
        public void Replaced_services_are_scoped_appropriately()
        {
            var services = new ServiceCollection();
            services
                .AddEntityFramework()
                .GetInfrastructure()
                .AddSingleton<IModelSource, FakeModelSource>()
                .AddScoped<IStateManager, FakeStateManager>();

            var provider = services.BuildServiceProvider();

            var context = new EarlyLearningCenter(provider);

            var modelSource = context.GetService<IModelSource>();

            context.Dispose();

            context = new EarlyLearningCenter(provider);

            var stateManager = context.GetService<IStateManager>();

            Assert.Same(stateManager, context.GetService<IStateManager>());

            Assert.Same(modelSource, context.GetService<IModelSource>());

            context.Dispose();

            context = new EarlyLearningCenter(provider);

            Assert.NotSame(stateManager, context.GetService<IStateManager>());

            Assert.Same(modelSource, context.GetService<IModelSource>());

            context.Dispose();
        }
        public void Replaced_services_are_scoped_appropriately()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework()
            .UseClrCollectionAccessorSource <FakeClrCollectionAccessorSource>()
            .UseClrPropertyGetterSource <FakeClrPropertyGetterSource>()
            .UseClrPropertySetterSource <FakeClrPropertySetterSource>()
            .UseEntityKeyFactorySource <FakeEntityKeyFactorySource>()
            .UseEntityMaterializerSource <FakeEntityMaterializerSource>()
            .UseDbSetFinder <FakeDbSetFinder>()
            .UseDbSetInitializer <FakeDbSetInitializer>()
            .UseEntityStateListener <FakeEntityStateListener>()
            .UseLoggerFactory <FakeLoggerFactory>()
            .UseModelSource <FakeModelSource>()
            .UseStateEntryFactory <FakeStateEntryFactory>()
            .UseStateEntryNotifier <FakeStateEntryNotifier>()
            .UseContextSets <FakeContextSets>()
            .UseStateManager <FakeStateManager>()
            .UseEntityStateListener <FakeNavigationFixer>();

            var provider = services.BuildServiceProvider();

            StateEntryFactory    stateEntryFactory;
            StateEntryNotifier   stateEntryNotifier;
            ContextSets          contextSets;
            StateManager         stateManager;
            IEntityStateListener entityStateListener;

            var context       = new EarlyLearningCenter(provider);
            var configuration = context.Configuration;

            var clrCollectionAccessorSource = configuration.Services.ServiceProvider.GetService <ClrCollectionAccessorSource>();
            var clrPropertyGetterSource     = configuration.Services.ClrPropertyGetterSource;
            var clrPropertySetterSource     = configuration.Services.ClrPropertySetterSource;
            var entityKeyFactorySource      = configuration.Services.EntityKeyFactorySource;
            var entityMaterializerSource    = configuration.Services.ServiceProvider.GetService <EntityMaterializerSource>();
            var setFinder      = configuration.Services.ServiceProvider.GetService <DbSetFinder>();
            var setInitializer = configuration.Services.ServiceProvider.GetService <DbSetInitializer>();
            var loggerFactory  = configuration.Services.ServiceProvider.GetService <ILoggerFactory>();
            var modelSource    = configuration.Services.ModelSource;

            context.Dispose();

            context       = new EarlyLearningCenter(provider);
            configuration = context.Configuration;

            stateEntryFactory   = configuration.Services.StateEntryFactory;
            stateEntryNotifier  = configuration.Services.StateEntryNotifier;
            contextSets         = configuration.Services.ContextSets;
            stateManager        = configuration.Services.StateManager;
            entityStateListener = configuration.Services.EntityStateListeners.OfType <FakeNavigationFixer>().Single();

            Assert.Same(stateEntryFactory, configuration.Services.StateEntryFactory);
            Assert.Same(stateEntryNotifier, configuration.Services.StateEntryNotifier);
            Assert.Same(contextSets, configuration.Services.ContextSets);
            Assert.Same(stateManager, configuration.Services.StateManager);
            Assert.Same(entityStateListener, configuration.Services.EntityStateListeners.OfType <FakeNavigationFixer>().Single());

            Assert.Same(clrCollectionAccessorSource, configuration.Services.ServiceProvider.GetService <ClrCollectionAccessorSource>());
            Assert.Same(clrPropertyGetterSource, configuration.Services.ClrPropertyGetterSource);
            Assert.Same(clrPropertySetterSource, configuration.Services.ClrPropertySetterSource);
            Assert.Same(entityKeyFactorySource, configuration.Services.EntityKeyFactorySource);
            Assert.Same(entityMaterializerSource, configuration.Services.ServiceProvider.GetService <EntityMaterializerSource>());
            Assert.Same(setFinder, configuration.Services.ServiceProvider.GetService <DbSetFinder>());
            Assert.Same(setInitializer, configuration.Services.ServiceProvider.GetService <DbSetInitializer>());
            Assert.Same(loggerFactory, configuration.Services.ServiceProvider.GetService <ILoggerFactory>());
            Assert.Same(modelSource, configuration.Services.ModelSource);

            context.Dispose();

            context       = new EarlyLearningCenter(provider);
            configuration = context.Configuration;

            Assert.NotSame(stateEntryFactory, configuration.Services.StateEntryFactory);
            Assert.NotSame(stateEntryNotifier, configuration.Services.StateEntryNotifier);
            Assert.NotSame(contextSets, configuration.Services.ContextSets);
            Assert.NotSame(stateManager, configuration.Services.StateManager);
            Assert.NotSame(entityStateListener, configuration.Services.EntityStateListeners.OfType <FakeNavigationFixer>().Single());

            Assert.Same(clrCollectionAccessorSource, configuration.Services.ServiceProvider.GetService <ClrCollectionAccessorSource>());
            Assert.Same(clrPropertyGetterSource, configuration.Services.ClrPropertyGetterSource);
            Assert.Same(clrPropertySetterSource, configuration.Services.ClrPropertySetterSource);
            Assert.Same(entityKeyFactorySource, configuration.Services.EntityKeyFactorySource);
            Assert.Same(entityMaterializerSource, configuration.Services.ServiceProvider.GetService <EntityMaterializerSource>());
            Assert.Same(setFinder, configuration.Services.ServiceProvider.GetService <DbSetFinder>());
            Assert.Same(setInitializer, configuration.Services.ServiceProvider.GetService <DbSetInitializer>());
            Assert.Same(loggerFactory, configuration.Services.ServiceProvider.GetService <ILoggerFactory>());
            Assert.Same(modelSource, configuration.Services.ModelSource);

            context.Dispose();
        }
Example #11
0
        public void Replaced_services_are_scoped_appropriately()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework()
                .UseClrCollectionAccessorSource<FakeClrCollectionAccessorSource>()
                .UseClrPropertyGetterSource<FakeClrPropertyGetterSource>()
                .UseClrPropertySetterSource<FakeClrPropertySetterSource>()
                .UseEntityKeyFactorySource<FakeEntityKeyFactorySource>()
                .UseEntityMaterializerSource<FakeEntityMaterializerSource>()
                .UseDbSetFinder<FakeDbSetFinder>()
                .UseDbSetInitializer<FakeDbSetInitializer>()
                .UseEntityStateListener<FakeEntityStateListener>()
                .UseLoggerFactory<FakeLoggerFactory>()
                .UseModelSource<FakeModelSource>()
                .UseStateEntryFactory<FakeStateEntryFactory>()
                .UseStateEntryNotifier<FakeStateEntryNotifier>()
                .UseContextSets<FakeContextSets>()
                .UseStateManager<FakeStateManager>()
                .UseEntityStateListener<FakeNavigationFixer>();

            var provider = services.BuildServiceProvider();

            StateEntryFactory stateEntryFactory;
            StateEntryNotifier stateEntryNotifier;
            ContextSets contextSets;
            StateManager stateManager;
            IEntityStateListener entityStateListener;

            var context = new EarlyLearningCenter(provider);
            var configuration = context.Configuration;

            var clrCollectionAccessorSource = configuration.Services.ServiceProvider.GetService<ClrCollectionAccessorSource>();
            var clrPropertyGetterSource = configuration.Services.ClrPropertyGetterSource;
            var clrPropertySetterSource = configuration.Services.ClrPropertySetterSource;
            var entityKeyFactorySource = configuration.Services.EntityKeyFactorySource;
            var entityMaterializerSource = configuration.Services.ServiceProvider.GetService<EntityMaterializerSource>();
            var setFinder = configuration.Services.ServiceProvider.GetService<DbSetFinder>();
            var setInitializer = configuration.Services.ServiceProvider.GetService<DbSetInitializer>();
            var loggerFactory = configuration.Services.ServiceProvider.GetService<ILoggerFactory>();
            var modelSource = configuration.Services.ModelSource;

            context.Dispose();

            context = new EarlyLearningCenter(provider);
            configuration = context.Configuration;

            stateEntryFactory = configuration.Services.StateEntryFactory;
            stateEntryNotifier = configuration.Services.StateEntryNotifier;
            contextSets = configuration.Services.ContextSets;
            stateManager = configuration.Services.StateManager;
            entityStateListener = configuration.Services.EntityStateListeners.OfType<FakeNavigationFixer>().Single();

            Assert.Same(stateEntryFactory, configuration.Services.StateEntryFactory);
            Assert.Same(stateEntryNotifier, configuration.Services.StateEntryNotifier);
            Assert.Same(contextSets, configuration.Services.ContextSets);
            Assert.Same(stateManager, configuration.Services.StateManager);
            Assert.Same(entityStateListener, configuration.Services.EntityStateListeners.OfType<FakeNavigationFixer>().Single());

            Assert.Same(clrCollectionAccessorSource, configuration.Services.ServiceProvider.GetService<ClrCollectionAccessorSource>());
            Assert.Same(clrPropertyGetterSource, configuration.Services.ClrPropertyGetterSource);
            Assert.Same(clrPropertySetterSource, configuration.Services.ClrPropertySetterSource);
            Assert.Same(entityKeyFactorySource, configuration.Services.EntityKeyFactorySource);
            Assert.Same(entityMaterializerSource, configuration.Services.ServiceProvider.GetService<EntityMaterializerSource>());
            Assert.Same(setFinder, configuration.Services.ServiceProvider.GetService<DbSetFinder>());
            Assert.Same(setInitializer, configuration.Services.ServiceProvider.GetService<DbSetInitializer>());
            Assert.Same(loggerFactory, configuration.Services.ServiceProvider.GetService<ILoggerFactory>());
            Assert.Same(modelSource, configuration.Services.ModelSource);

            context.Dispose();

            context = new EarlyLearningCenter(provider);
            configuration = context.Configuration;

            Assert.NotSame(stateEntryFactory, configuration.Services.StateEntryFactory);
            Assert.NotSame(stateEntryNotifier, configuration.Services.StateEntryNotifier);
            Assert.NotSame(contextSets, configuration.Services.ContextSets);
            Assert.NotSame(stateManager, configuration.Services.StateManager);
            Assert.NotSame(entityStateListener, configuration.Services.EntityStateListeners.OfType<FakeNavigationFixer>().Single());

            Assert.Same(clrCollectionAccessorSource, configuration.Services.ServiceProvider.GetService<ClrCollectionAccessorSource>());
            Assert.Same(clrPropertyGetterSource, configuration.Services.ClrPropertyGetterSource);
            Assert.Same(clrPropertySetterSource, configuration.Services.ClrPropertySetterSource);
            Assert.Same(entityKeyFactorySource, configuration.Services.EntityKeyFactorySource);
            Assert.Same(entityMaterializerSource, configuration.Services.ServiceProvider.GetService<EntityMaterializerSource>());
            Assert.Same(setFinder, configuration.Services.ServiceProvider.GetService<DbSetFinder>());
            Assert.Same(setInitializer, configuration.Services.ServiceProvider.GetService<DbSetInitializer>());
            Assert.Same(loggerFactory, configuration.Services.ServiceProvider.GetService<ILoggerFactory>());
            Assert.Same(modelSource, configuration.Services.ModelSource);

            context.Dispose();
        }