public void Main(string[] args) { var connectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;"; IServiceCollection services = new ServiceCollection(); services.AddEntityFramework() .AddSqlServer() .AddDbContext<DataContext>(options => options.UseSqlServer(connectionString)); var serviceProvider = services.BuildServiceProvider(); //serviceProvider.Add var context = serviceProvider.GetService<DataContext>(); var categoryCount = context.Categories.Count(); Console.WriteLine($"category count is {categoryCount}"); var newCat = new Category { Name = $"Category {categoryCount + 1}", Description = $"Category {categoryCount + 1} description" }; context.Add(newCat); //context.SaveChanges(); var count = context.SaveChanges(); Console.WriteLine($"change count is {count}"); context.Dispose(); Console.WriteLine("Hello, world!"); }
public ActivityApiControllerTest() { if (_serviceProvider == null) { var services = new ServiceCollection(); // Add Configuration to the Container var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddEnvironmentVariables(); IConfiguration configuration = builder.Build(); services.AddSingleton(x => configuration); // Add EF (Full DB, not In-Memory) services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase()); // Setup hosting environment IHostingEnvironment hostingEnvironment = new HostingEnvironment(); hostingEnvironment.EnvironmentName = "Development"; services.AddSingleton(x => hostingEnvironment); _serviceProvider = services.BuildServiceProvider(); } }
public void ConfigureServices() { IServiceProvider mainProv = CallContextServiceLocator.Locator.ServiceProvider; IApplicationEnvironment appEnv = mainProv.GetService<IApplicationEnvironment>(); IRuntimeEnvironment runtimeEnv = mainProv.GetService<IRuntimeEnvironment>(); ILoggerFactory logFactory = new LoggerFactory(); logFactory.AddConsole(LogLevel.Information); ServiceCollection sc = new ServiceCollection(); sc.AddInstance(logFactory); sc.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); sc.AddEntityFramework() .AddSqlite() .AddDbContext<StarDbContext>(); sc.AddSingleton<ILibraryManager, LibraryManager>(factory => mainProv.GetService<ILibraryManager>() as LibraryManager); sc.AddSingleton<ICache, Cache>(factory => new Cache(new CacheContextAccessor())); sc.AddSingleton<IExtensionAssemblyLoader, ExtensionAssemblyLoader>(); sc.AddSingleton<IStarLibraryManager, StarLibraryManager>(); sc.AddSingleton<PluginLoader>(); sc.AddSingleton(factory => mainProv.GetService<IAssemblyLoadContextAccessor>()); sc.AddInstance(appEnv); sc.AddInstance(runtimeEnv); Services = sc; ServiceProvider = sc.BuildServiceProvider(); }
public void Can_get_default_services() { var services = new ServiceCollection(); services.AddEntityFramework().AddSqlServer(); // Relational Assert.True(services.Any(sd => sd.ServiceType == typeof(DatabaseBuilder))); Assert.True(services.Any(sd => sd.ServiceType == typeof(RelationalObjectArrayValueReaderFactory))); Assert.True(services.Any(sd => sd.ServiceType == typeof(RelationalTypedValueReaderFactory))); Assert.True(services.Any(sd => sd.ServiceType == typeof(CommandBatchPreparer))); Assert.True(services.Any(sd => sd.ServiceType == typeof(ModificationCommandComparer))); Assert.True(services.Any(sd => sd.ServiceType == typeof(GraphFactory))); // SQL Server dingletones Assert.True(services.Any(sd => sd.ServiceType == typeof(DataStoreSource))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerSqlGenerator))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlStatementExecutor))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerTypeMapper))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerBatchExecutor))); Assert.True(services.Any(sd => sd.ServiceType == typeof(ModificationCommandBatchFactory))); // SQL Server scoped Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerDataStore))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerConnection))); Assert.True(services.Any(sd => sd.ServiceType == typeof(ModelDiffer))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerMigrationOperationSqlGeneratorFactory))); Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerDataStoreCreator))); Assert.True(services.Any(sd => sd.ServiceType == typeof(MigrationAssembly))); Assert.True(services.Any(sd => sd.ServiceType == typeof(HistoryRepository))); Assert.True(services.Any(sd => sd.ServiceType == typeof(Migrator))); }
public void Main(string[] args) { var services = new ServiceCollection(); services .AddEntityFramework() .AddSqlServer() .AddDbContext<UserDbContext>() .AddEntityFrameworkMixins(); var provider = services.BuildServiceProvider(); var context = provider.GetRequiredService<UserDbContext>(); var users = ( from u in context.Users where u.Mixin<Author>().GooglePlusProfile != null select u //u.Mixin<Author>() ).ToList(); var user = users.FirstOrDefault(); if (user != null) { var author = user.Mixin<Author>(); // You can make changes here: author.IsAwesome = true; // Save changes context.SaveChanges(); } }
private static async Task Can_use_an_existing_closed_connection_test(bool openConnection) { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (var store = await SqlServerNorthwindContext.GetSharedStoreAsync()) { var openCount = 0; var closeCount = 0; var disposeCount = 0; using (var connection = new SqlConnection(store.Connection.ConnectionString)) { if (openConnection) { await connection.OpenAsync(); } connection.StateChange += (_, a) => { if (a.CurrentState == ConnectionState.Open) { openCount++; } else if (a.CurrentState == ConnectionState.Closed) { closeCount++; } }; #if !DNXCORE50 connection.Disposed += (_, __) => disposeCount++; #endif using (var context = new NorthwindContext(serviceProvider, connection)) { Assert.Equal(91, await context.Customers.CountAsync()); } if (openConnection) { Assert.Equal(ConnectionState.Open, connection.State); Assert.Equal(0, openCount); Assert.Equal(0, closeCount); } else { Assert.Equal(ConnectionState.Closed, connection.State); Assert.Equal(1, openCount); Assert.Equal(1, closeCount); } Assert.Equal(0, disposeCount); } } }
public void Can_get_default_services() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryStore(); Assert.True(services.Any(sd => sd.ServiceType == typeof(InMemoryDataStore))); Assert.True(services.Any(sd => sd.ServiceType == typeof(DataStoreSource))); }
public GenreMenuComponentTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryStore() .AddDbContext<MusicStoreContext>(); _serviceProvider = services.BuildServiceProvider(); }
public static DbContextConfiguration CreateConfiguration() { var serviceCollection = new ServiceCollection(); serviceCollection.AddEntityFramework().AddSqlServer(); return new DbContext(serviceCollection.BuildServiceProvider(), new DbContextOptions() .UseSqlServer("Server=(localdb)\v11.0;Database=SqlServerConnectionTest;Trusted_Connection=True;")) .Configuration; }
public CheckoutControllerTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryStore() .AddDbContext<MusicStoreContext>(); _serviceProvider = services.BuildServiceProvider(); }
public CartSummaryComponentTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase()); _serviceProvider = services.BuildServiceProvider(); }
public void ConfigureServices() { ServiceCollection sc = new ServiceCollection(); sc.AddEntityFramework() .AddSqlite() .AddDbContext<StarDbContext>(); sc.AddTransient<StarDbContext>(); sc.BuildServiceProvider(); }
public static InMemoryContext CreateContext() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryDatabase(); var serviceProvider = services.BuildServiceProvider(); var db = new InMemoryContext(); db.Database.EnsureCreated(); return db; }
public static IServiceProvider Create() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase()); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; }
public void AddRelational_does_not_replace_services_already_registered() { var services = new ServiceCollection() .AddSingleton<IComparer<ModificationCommand>, FakeModificationCommandComparer>(); services.AddEntityFramework().AddRelational(); var serviceProvider = services.BuildServiceProvider(); Assert.IsType<FakeModificationCommandComparer>(serviceProvider.GetRequiredService<IComparer<ModificationCommand>>()); }
public async Task CanCreateUsingAddRoleManager() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryStore(); var store = new RoleStore<IdentityRole>(new InMemoryContext()); services.AddIdentity<InMemoryUser, IdentityRole>().AddRoleStore(() => store); var provider = services.BuildServiceProvider(); var manager = provider.GetService<RoleManager<IdentityRole>>(); Assert.NotNull(manager); IdentityResultAssert.IsSuccess(await manager.CreateAsync(new IdentityRole("arole"))); }
public ActivityApiControllerTest() { if (_serviceProvider == null) { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryStore() .AddDbContext<PrepOpsContext>(options => options.UseInMemoryStore()); _serviceProvider = services.BuildServiceProvider(); } }
public ShoppingCartControllerTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase()); services.AddMvc(); _serviceProvider = services.BuildServiceProvider(); }
public async Task CanCreateRoleWithSingletonManager() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryStore(); services.AddTransient<InMemoryContext>(); services.AddTransient<IRoleStore<IdentityRole>, RoleStore<IdentityRole, InMemoryContext>>(); services.AddIdentity<IdentityUser, IdentityRole>(); services.AddSingleton<RoleManager<IdentityRole>>(); var provider = services.BuildServiceProvider(); var manager = provider.GetRequiredService<RoleManager<IdentityRole>>(); Assert.NotNull(manager); IdentityResultAssert.IsSuccess(await manager.CreateAsync(new IdentityRole("someRole"))); }
public void Services_wire_up_correctly() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryStore(); var serviceProvider = services.BuildServiceProvider(); using (var context = new DbContext(serviceProvider, new DbContextOptions())) { var scopedProvider = context.Configuration.Services.ServiceProvider; Assert.NotNull(scopedProvider.GetService<InMemoryDataStore>()); Assert.NotNull(scopedProvider.GetService<DataStoreSource>()); } }
public static IdentityDbContext CreateContext(bool delete = false) { var services = new ServiceCollection(); services.AddEntityFramework().AddSqlServer(); var serviceProvider = services.BuildServiceProvider(); var db = new IdentityDbContext(serviceProvider, ConnectionString); if (delete) { db.Database.EnsureDeleted(); } db.Database.EnsureCreated(); return db; }
public async Task Can_use_actual_connection_string_in_OnConfiguring() { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (await SqlServerNorthwindContext.GetSharedStoreAsync()) { using (var context = new NorthwindContext(serviceProvider, SqlServerNorthwindContext.ConnectionString)) { Assert.Equal(91, await context.Customers.CountAsync()); } } }
public void Can_specify_connection_string_in_OnConfiguring() { var serviceCollection = new ServiceCollection(); serviceCollection .AddEntityFramework() .AddSqlServer() .AddDbContext<StringInOnConfiguringContext>(); var serviceProvider = serviceCollection.BuildServiceProvider(); using (SqlServerNorthwindContext.GetSharedStore()) { using (var context = serviceProvider.GetRequiredService<StringInOnConfiguringContext>()) { Assert.True(context.Customers.Any()); } } }
public ActivityApiControllerTest() { if (_serviceProvider == null) { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase()); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("testConfig.json"); IConfiguration configuration = builder.Build(); services.AddSingleton(x => configuration); _serviceProvider = services.BuildServiceProvider(); } }
public virtual IServiceProvider GetOrAdd(IDbContextOptionsExtensions options) { var services = new ServiceCollection(); var builder = services.AddEntityFramework(); foreach (var extension in options.Extensions) { extension.ApplyServices(builder); } // TODO: Consider more robust hashing algorithm // Note that no cryptographic algorithm is available on all of phone/store/k/desktop unchecked { var key = services.Aggregate(0, (t, d) => (t * 397) ^ CalculateHash(d).GetHashCode()); return _configurations.GetOrAdd(key, k => services.BuildServiceProvider()); } }
public virtual IServiceProvider GetOrAdd([NotNull] IDbContextOptions options) { var services = new ServiceCollection(); var builder = services.AddEntityFramework(); foreach (var extension in options.Extensions) { extension.ApplyServices(builder); } // Decided that this hashing algorithm is robust enough. See issue #762. unchecked { var key = services.Aggregate(0, (t, d) => (t * 397) ^ CalculateHash(d).GetHashCode()); return _configurations.GetOrAdd(key, k => services.BuildServiceProvider()); } }
public void Each_context_gets_new_scoped_context_configuration() { var services = new ServiceCollection(); services.AddEntityFramework(); var serviceProvider = services.BuildServiceProvider(); DbContextConfiguration configuration; using (var context = new DbContext(serviceProvider)) { configuration = context.Configuration; Assert.Same(configuration, context.Configuration); } using (var context = new DbContext(serviceProvider)) { Assert.NotSame(configuration, context.Configuration); } }
public ActivityApiControllerTest() { if (_serviceProvider == null) { var services = new ServiceCollection(); // Add EF (Full DB, not In-Memory) services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase()); // Setup hosting environment IHostingEnvironment hostingEnvironment = new HostingEnvironment(); hostingEnvironment.EnvironmentName = "Development"; services.AddSingleton(x => hostingEnvironment); _serviceProvider = services.BuildServiceProvider(); } }
public ManageControllerTest() { var services = new ServiceCollection(); services.AddEntityFramework() .AddInMemoryStore() .AddDbContext<MusicStoreContext>(); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<MusicStoreContext>(); // IHttpContextAccessor is required for SignInManager, and UserManager services.AddInstance<IHttpContextAccessor>( new HttpContextAccessor() { HttpContext = new TestHttpContext(), }); _serviceProvider = services.BuildServiceProvider(); }
public void Can_save_and_query_with_explicit_services_and_OnConfiguring() { var services = new ServiceCollection(); services.AddEntityFramework().AddInMemoryStore(); var serviceProvider = services.BuildServiceProvider(); using (var context = new BlogContext(serviceProvider)) { context.Blogs.Add(new Blog { Name = "The Waffle Cart" }); context.SaveChanges(); } using (var context = new BlogContext(serviceProvider)) { var blog = context.Blogs.SingleOrDefault(); Assert.NotEqual(0, blog.Id); Assert.Equal("The Waffle Cart", blog.Name); } }