public BuiltInDataTypesInMemoryFixture()
        {
            _testStore = new InMemoryTestStore();
            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .ServiceCollection()
                .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;
        }
        public InheritanceSqliteFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddSqlite()
                    .ServiceCollection()
                    .AddSingleton(TestSqliteModelSource.GetFactory(OnModelCreating))
                    .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                    .BuildServiceProvider();

            var testStore = SqliteTestStore.CreateScratch();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlite(testStore.Connection);
            _options = optionsBuilder.Options;

            // TODO: Do this via migrations & update pipeline

            testStore.ExecuteNonQuery(@"
                DROP TABLE IF EXISTS Country;
                DROP TABLE IF EXISTS Animal;

                CREATE TABLE Country (
                    Id int NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL
                );

                CREATE TABLE Animal (
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NOT NULL ,
                    IsFlightless bit NOT NULL,
                    EagleId nvarchar(100),
                    'Group' int,
                    FoundOn tinyint,
                    Discriminator nvarchar(255),

                    FOREIGN KEY(countryId) REFERENCES Country(Id),
                    FOREIGN KEY(EagleId) REFERENCES Animal(Species)
                );
            ");

            using (var context = CreateContext())
            {
                SeedData(context);
            }
            TestSqlLoggerFactory.Reset();
        }
        public MappingQuerySqlServerFixture()
        {
            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                .BuildServiceProvider();

            _testDatabase = SqlServerNorthwindContext.GetSharedStore();

            var optionsBuilder = new EntityOptionsBuilder().UseModel(CreateModel());
            optionsBuilder.UseSqlServer(_testDatabase.Connection.ConnectionString);
            _options = optionsBuilder.Options;
        }
        public NorthwindQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = CreateContext())
            {
                NorthwindData.Seed(context);
            }
        }
        public NorthwindSprocQuerySqlServerFixture()
        {
            _testStore = SqlServerNorthwindContext.GetSharedStore();

            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(_testStore.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            _serviceProvider.GetRequiredService<ILoggerFactory>()
                .MinimumLevel = LogLevel.Debug;
        }
        public OneToOneQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                AddTestData(context);
            }
        }
        public BuiltInDataTypesSqlServerFixture()
        {
            _testStore = SqlServerTestStore.CreateScratch();

            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(_testStore.Connection);

            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();
            }
        }
Esempio n. 8
0
        async Task <EntityViewModel <Question, Answer> > GetViewModel(
            EntityOptions options)
        {
            if (options.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.Id));
            }

            var topic = await _entityStore.GetByIdAsync(options.Id);

            if (topic == null)
            {
                throw new ArgumentNullException();
            }

            // Return view model
            return(new EntityViewModel <Question, Answer>
            {
                Options = options,
                Entity = topic
            });
        }
Esempio n. 9
0
        async Task <EntityViewModel <Issue, Comment> > GetViewModel(
            EntityOptions options)
        {
            if (options.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.Id));
            }

            var entity = await _entityStore.GetByIdAsync(options.Id);

            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            // Return view model
            return(new EntityViewModel <Issue, Comment>
            {
                Options = options,
                Entity = entity
            });
        }
Esempio n. 10
0
        public BuiltInDataTypesSqlServerFixture()
        {
            _testStore = SqlServerTestStore.CreateScratch();

            _serviceProvider = new ServiceCollection()
                               .AddEntityFramework()
                               .AddSqlServer()
                               .ServiceCollection()
                               .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                               .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(_testStore.Connection);

            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();
            }
        }
Esempio n. 11
0
        // -----------------
        // Get User
        // -----------------

        public async Task <IActionResult> GetEntity(EntityOptions opts)
        {
            if (opts == null)
            {
                opts = new EntityOptions();
            }

            if (opts.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(opts.Id));
            }

            // Get authenticated user
            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // Get entity checking to ensure the entity is visible
            var entities = await _entityStore.QueryAsync()
                           .Select <EntityQueryParams>(q =>
            {
                q.UserId.Equals(user?.Id ?? 0);
                q.Id.Equals(opts.Id);
                q.HideSpam.True();
                q.HideHidden.True();
                q.HideDeleted.True();
                q.HidePrivate.True();
            })
                           .ToList();

            // Ensure entity exists
            if (entities?.Data == null)
            {
                return(View());
            }

            // Return view
            return(View(entities.Data[0]));
        }
        public OneToOneQuerySqliteFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddSqlite()
                    .ServiceCollection()
                    .AddSingleton(TestSqliteModelSource.GetFactory(OnModelCreating))
                    .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                    .BuildServiceProvider();

            var database = SqliteTestStore.CreateScratch();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlite(database.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();

                AddTestData(context);
            }
        }
 public BloggingContextWithSnapshotThatThrows(IServiceProvider provider, EntityOptions options)
     : base(provider, options)
 { }
Esempio n. 14
0
 public ConfigurationContext(EntityOptions options) : base(options)
 {
 }
Esempio n. 15
0
        private void InitializeSets(IServiceProvider serviceProvider, EntityOptions options)
        {
            serviceProvider = serviceProvider ?? ServiceProviderCache.Instance.GetOrAdd(options);

            serviceProvider.GetRequiredService <IDbSetInitializer>().InitializeSets(this);
        }
Esempio n. 16
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="DbContext" /> with the specified options. The
        ///     <see cref="OnConfiguring(EntityOptionsBuilder)" /> method will still be called to allow further
        ///     configuration of the options.
        /// </summary>
        /// <param name="options">The options for this context.</param>
        public DbContext([NotNull] EntityOptions options)
        {
            Check.NotNull(options, nameof(options));

            Initialize(DbContextActivator.ServiceProvider, options);
        }
 protected MonsterContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 protected NorthwindContextBase(EntityOptions options)
     : base(options)
 {
 }
Esempio n. 19
0
 private EntityOptions <UnkoolContext> GenericCheck(EntityOptions <UnkoolContext> options) => options;
 public StoreGeneratedContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public NorthwindContext(EntityOptions options)
     : base(options)
 {
     Assert.NotNull(options);
 }
 public NorthwindContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
Esempio n. 23
0
 public InjectConfigurationBlogContext(EntityOptions options)
     : base(options)
 {
     Assert.NotNull(options);
 }
Esempio n. 24
0
 public InjectDifferentConfigurationsAccountContext(IServiceProvider serviceProvider, EntityOptions <InjectDifferentConfigurationsAccountContext> options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
 public GearsOfWarContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public CrossStoreContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
        void IOptionsBuilderExtender.AddOrUpdateExtension <TExtension>(TExtension extension)
        {
            Check.NotNull(extension, nameof(extension));

            _options = _options.WithExtension(extension);
        }
        public EntityOptionsBuilder([NotNull] EntityOptions options)
        {
            Check.NotNull(options, nameof(options));

            _options = options;
        }
 public ComplexNavigationsContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public NorthwindContext(EntityOptions options)
     : base(options)
 {
 }
 public NonGenericOptionsContext(EntityOptions options)
     : base(options)
 {
     _options = options;
 }
 public ChangedOnlyMonsterContext(IServiceProvider serviceProvider, EntityOptions options, Action <ModelBuilder> onModelCreating)
     : base(serviceProvider, options, onModelCreating)
 {
 }
 private EntityOptions<UnkoolContext> GenericCheck(EntityOptions<UnkoolContext> options) => options;
 public EntityOptionsBuilder([NotNull] EntityOptions <TContext> options)
     : base(options)
 {
 }
Esempio n. 35
0
 public AnimalContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
Esempio n. 36
0
 public InjectContextAndConfigurationBlogContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
Esempio n. 37
0
 public EarlyLearningCenter(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public BloggingContextWithPendingModelChanges(IServiceProvider provider, EntityOptions options)
     : base(provider, options)
 { }
Esempio n. 39
0
 public ContextWithOptions(EntityOptions<ContextWithOptions> contextOptions)
     : base(contextOptions)
 {
 }
Esempio n. 40
0
        private IDbContextServices InitializeServices(IServiceProvider serviceProvider, EntityOptions options)
        {
            if (_initializing)
            {
                throw new InvalidOperationException(Strings.RecursiveOnConfiguring);
            }

            try
            {
                _initializing = true;

                var optionsBuilder = new EntityOptionsBuilder(options);
                OnConfiguring(optionsBuilder);

                var dataStores = serviceProvider?.GetService <IEnumerable <IDataStoreSource> >()?.ToList()
                                 ?? new List <IDataStoreSource>();

                if (dataStores.Count == 1 &&
                    !dataStores[0].IsConfigured(optionsBuilder.Options))
                {
                    dataStores[0].AutoConfigure(optionsBuilder);
                }

                var providerSource = serviceProvider != null ? ServiceProviderSource.Explicit : ServiceProviderSource.Implicit;

                serviceProvider = serviceProvider ?? ServiceProviderCache.Instance.GetOrAdd(optionsBuilder.Options);

                _loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
                _logger        = _loggerFactory.CreateLogger <DbContext>();
                _logger.LogDebug(Strings.DebugLogWarning(nameof(LogLevel.Debug), nameof(ILoggerFactory) + "." + nameof(ILoggerFactory.MinimumLevel), nameof(LogLevel) + "." + nameof(LogLevel.Verbose)));

                var scopedServiceProvider = serviceProvider
                                            .GetRequiredService <IServiceScopeFactory>()
                                            .CreateScope()
                                            .ServiceProvider;

                return(scopedServiceProvider
                       .GetRequiredService <IDbContextServices>()
                       .Initialize(scopedServiceProvider, optionsBuilder.Options, this, providerSource));
            }
            finally
            {
                _initializing = false;
            }
        }
 public NullSemanticsContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public BloggingContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
Esempio n. 43
0
        // -----------------
        // Index
        // Displays a summary from StopForumSpam.
        // -----------------

        public async Task <IActionResult> Index(EntityOptions opts)
        {
            if (opts == null)
            {
                opts = new EntityOptions();
            }

            // We always need an entity Id
            if (opts.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(opts.Id));
            }

            // We always need an entity
            var entity = await _entityStore.GetByIdAsync(opts.Id);

            if (entity == null)
            {
                return(NotFound());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, entity.CategoryId, Permissions.ViewStopForumSpam))
            {
                return(Unauthorized());
            }

            // Get reply
            IEntityReply reply = null;

            if (opts.ReplyId > 0)
            {
                reply = await _entityReplyStore.GetByIdAsync(opts.ReplyId);

                if (reply == null)
                {
                    return(NotFound());
                }
            }

            // Get user to validate
            var user = reply != null
                ? await GetUserToValidateAsync(reply)
                : await GetUserToValidateAsync(entity);

            // Ensure we found the user
            if (user == null)
            {
                return(NotFound());
            }

            // Build view model
            var viewModel = new StopForumSpamViewModel()
            {
                Options = opts,
                Checker = await _spamChecker.CheckAsync(user)
            };

            // Return view
            return(View(viewModel));
        }
Esempio n. 44
0
 public ExplicitServicesAndConfigBlogContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public InjectContextAndConfigurationBlogContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
 public JustAContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public InjectDifferentConfigurationsAccountContext(IServiceProvider serviceProvider, EntityOptions<InjectDifferentConfigurationsAccountContext> options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
 public JustAContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
Esempio n. 49
0
 public ImplicitServicesExplicitConfigBlogContext(EntityOptions options)
     : base(options)
 {
 }
Esempio n. 50
0
 public BloggingContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public SqliteNorthwindContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
Esempio n. 52
0
        // -----------------
        // AddSpammer
        // -----------------

        public async Task <IActionResult> AddSpammer(EntityOptions opts)
        {
            // Disable functionality within demo mode
            if (_platoOpts.DemoMode)
            {
                return(Unauthorized());
            }

            // Empty options
            if (opts == null)
            {
                opts = new EntityOptions();
            }

            // Validate
            if (opts.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(opts.Id));
            }

            // Get entity
            var entity = await _entityStore.GetByIdAsync(opts.Id);

            // Ensure the topic exists
            if (entity == null)
            {
                return(NotFound());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User,
                                                            entity.CategoryId, Permissions.AddToStopForumSpam))
            {
                return(Unauthorized());
            }

            // Get reply
            IEntityReply reply = null;

            if (opts.ReplyId > 0)
            {
                reply = await _entityReplyStore.GetByIdAsync(opts.ReplyId);
            }

            // Get user for reply or entity
            var user = reply != null
                ? await GetUserToValidateAsync(reply)
                : await GetUserToValidateAsync(entity);

            // Ensure we found the user
            if (user == null)
            {
                return(NotFound());
            }

            // Add spammer for reply or entity
            var result = await AddSpammerAsync(user);

            // Confirmation
            if (result.Success)
            {
                _alerter.Success(T["Spammer Added Successfully"]);
            }
            else
            {
                _alerter.Danger(!string.IsNullOrEmpty(result.Error)
                    ? T[result.Error]
                    : T["An unknown error occurred adding the user to the StopForumSpam database."]);
            }

            // Redirect back to reply
            if (reply != null)
            {
                return(Redirect(_contextFacade.GetRouteUrl(new RouteValueDictionary()
                {
                    ["area"] = "Plato.Docs",
                    ["controller"] = "Home",
                    ["action"] = "Reply",
                    ["opts.id"] = entity.Id,
                    ["opts.alias"] = entity.Alias,
                    ["opts.replyId"] = reply.Id
                })));
            }

            // Redirect back to entity
            return(Redirect(_contextFacade.GetRouteUrl(new RouteValueDictionary()
            {
                ["area"] = "Plato.Docs",
                ["controller"] = "Home",
                ["action"] = "Display",
                ["opts.id"] = entity.Id,
                ["opts.alias"] = entity.Alias
            })));
        }
 public NorthwindContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public ExplicitServicesAndConfigBlogContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
 }
 public NorthwindContext(IServiceProvider serviceProvider, EntityOptions options)
     : base(serviceProvider, options)
 {
     Assert.NotNull(serviceProvider);
     Assert.NotNull(options);
 }
 public InjectConfigurationBlogContext(EntityOptions options)
     : base(options)
 {
     Assert.NotNull(options);
 }
 public NorthwindContext(EntityOptions options)
     : base(options)
 {
     Assert.NotNull(options);
 }
 public ImplicitServicesExplicitConfigBlogContext(EntityOptions options)
     : base(options)
 {
 }
 public NorthwindContext(EntityOptions options)
     : base(options)
 {
 }