当前位置: 首页>>代码示例>>C#>>正文


C# ServiceCollection.AddEntityFramework方法代码示例

本文整理汇总了C#中ServiceCollection.AddEntityFramework方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddEntityFramework方法的具体用法?C# ServiceCollection.AddEntityFramework怎么用?C# ServiceCollection.AddEntityFramework使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.AddEntityFramework方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestBase

        public TestBase()
        {
            if (ServiceProvider == null)
            {
                var services = new ServiceCollection();

                // set up empty in-memory test db
                services.AddEntityFramework()
                    .AddInMemoryDatabase()
                    .AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase());

                // add identity service
                services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<AllReadyContext>();
                var context = new DefaultHttpContext();
                context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
                services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context });

                // Setup hosting environment
                IHostingEnvironment hostingEnvironment = new HostingEnvironment();
                hostingEnvironment.EnvironmentName = "Development";
                services.AddSingleton(x => hostingEnvironment);

                // set up service provider for tests
                ServiceProvider = services.BuildServiceProvider();
            }
        }
开发者ID:codethug,项目名称:allReady,代码行数:27,代码来源:TestBase.cs

示例2: HomeControllerTests

        public HomeControllerTests()
        {
            // Database setup
            var services = new ServiceCollection();
            services.AddEntityFramework()
                    .AddSqlServer()
                    .AddInMemoryDatabase()
                    .AddDbContext<DataDbContext>(options =>
                        options.UseInMemoryDatabase()
                    );

            // Dependencies initializations
            _pageConfiguration = new FakePageConfiguration();

            var optionsBuilder = new DbContextOptionsBuilder<DataDbContext>();
            optionsBuilder.UseInMemoryDatabase();
            _dataDbContext = new DataDbContext(optionsBuilder.Options);

            _contentRepository = new ContentRepository(_dataDbContext);
            _humanReadableContentService = new HumanReadableContentService(_pageConfiguration, _contentRepository);

            _languageManipulationService = new LanguageManipulationService();

            // Controller initialization
            _homeController = new PersonalWebsite.Controllers.HomeController(
                _pageConfiguration,
                _humanReadableContentService,
                _languageManipulationService
            );
        }
开发者ID:nettsundere,项目名称:PersonalWebsite,代码行数:30,代码来源:HomeControllerTests.cs

示例3: Can_use_an_existing_closed_connection_test

        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 = SqlServerNorthwindContext.GetSharedStore())
            {
                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);
                }
            }
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:59,代码来源:ExistingConnectionTest.cs

示例4: GenreMenuComponentTest

        public GenreMenuComponentTest()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework()
                      .AddInMemoryDatabase()
                      .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:leloulight,项目名称:MusicStore,代码行数:9,代码来源:GenreMenuComponentTest.cs

示例5: BlogServiceTestsAdvanced

 public BlogServiceTestsAdvanced()
 {
     // Create a service collection to be shared by all test methods
     _serviceCollection = new ServiceCollection();
     _serviceCollection
         .AddEntityFramework()
         .AddInMemoryDatabase()
         .AddDbContext<BloggingContext>(c => c.UseInMemoryDatabase());
 }
开发者ID:ErikEJ,项目名称:EntityFramework.Docs,代码行数:9,代码来源:BlogServiceTestsAdvanced.cs

示例6: PersonDbContextTest

        public PersonDbContextTest()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework()
                      .AddInMemoryDatabase()
                      .AddDbContext<StoreDbContext>(options => options.UseInMemoryDatabase());

            serviceProvider = services.BuildServiceProvider();
        }
开发者ID:JensKlambauer,项目名称:MVC6_Testprojects,代码行数:10,代码来源:PersonDbContextTest.cs

示例7: BaseTest

        public BaseTest()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework()
                          .AddInMemoryDatabase()
                          .AddDbContext<ApplicationDbContext>();

            ServiceProvider = services.BuildServiceProvider();
        }
开发者ID:rguerreiro,项目名称:chutzpah-vso-test,代码行数:10,代码来源:BaseTest.cs

示例8: Main

        static void Main(string[] args)
        {
            var len = path.Length;
            var fileSystemItems = Directory.EnumerateFiles(Path.Combine(path, Elizabeth), "o_*", SearchOption.AllDirectories).AsParallel().Select(f =>
            {
                var text = File.ReadAllText(f.Replace("o_", ""));
                var sourceDirectoryLength = path.Length + Elizabeth.Length;
                var year = string.Concat(f.Skip(sourceDirectoryLength).Take(4));
                var month = string.Concat(f.Skip(sourceDirectoryLength + 5).TakeWhile(ch => ch != Convert.ToChar(@"\")));
                DateTime date;
                if (!DateTime.TryParseExact(year + "-" + month + "-" + "1", "yyyy-MMMM-d", null, System.Globalization.DateTimeStyles.None, out date))
                    date = DateTime.Now;
                var idx = text.IndexOf(rdfLi);
                string tags = (idx > -1)
                                        ? text.Substring(idx + rdfLi.Length, text.Substring(idx).IndexOf(endRdfLi) - rdfLi.Length)
                                        : string.Empty;

                var split2 = text.IndexOf(xmpRating);
                int rating = int.Parse(split2 > -1
                                                    ? text.Substring(split2 + xmpRating.Length, 1)
                                                    : "5");
                var uri = f.Substring(len);
                var image = System.Drawing.Image.FromFile(f);
                var heightRatio = (decimal)image.Height / (decimal)image.Width;
                return new PhotoInfo { uri = PhotoInfo.baseuri + uri.Replace("\\", "/"), heightRatio = heightRatio, rating = rating, tags = tags, date = date };
            }).ToDictionary(p => p.uri);
            var services = new ServiceCollection();
            services.AddEntityFramework().AddSqlServer().AddDbContext<PhotoContext>(optionsBuilder =>
                                                optionsBuilder.
                                                    UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString).
                                                    MigrationsAssembly("MVC6"));

            using (PhotoContext context = services.BuildServiceProvider().GetService<PhotoContext>())
            {
                var dbItems = context.PhotoInfos.ToDictionary(p => p.uri);
                context.RemoveRange(dbItems.Values.Where(p => !fileSystemItems.ContainsKey(p.uri)));
                foreach (var item in fileSystemItems.Values)
                {
                    PhotoInfo dbItem;
                    if (dbItems.TryGetValue(item.uri, out dbItem))
                    {
                        dbItem.date = item.date;
                        dbItem.rating = item.rating;
                        dbItem.tags = item.tags;
                        dbItem.heightRatio = item.heightRatio;
                        context.Update(dbItem);
                    }
                    else
                    {
                        context.Add(item);
                    }
                }
                context.SaveChanges();
            }
        }
开发者ID:kellerd,项目名称:EPhotoGallery,代码行数:55,代码来源:Program.cs

示例9: CreateContext

        public static InMemoryContext CreateContext()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework().AddInMemoryDatabase();
            var serviceProvider = services.BuildServiceProvider();

            var db = new InMemoryContext();
            db.Database.EnsureCreated();

            return db;
        }
开发者ID:adwardliu,项目名称:Identity,代码行数:11,代码来源:TestIdentityFactory.cs

示例10: Create

		public static IServiceProvider Create()
		{
			var services = new ServiceCollection();

			services.AddEntityFramework()
					  .AddInMemoryDatabase()
					  .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());

			var serviceProvider = services.BuildServiceProvider();
			return serviceProvider;
        }
开发者ID:IvanTyapkinSSW,项目名称:musicstore-api-aspnet5,代码行数:11,代码来源:ServiceProviderFactory.cs

示例11: ShoppingCartControllerTest

        public ShoppingCartControllerTest()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework()
                      .AddInMemoryDatabase()
                      .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());

            services.AddMvc();

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:leloulight,项目名称:MusicStore,代码行数:12,代码来源:ShoppingCartControllerTest.cs

示例12: GetServiceProvider

		public static IServiceProvider GetServiceProvider()
		{
			var services = new ServiceCollection();
			services.AddMvc();
			services.AddEntityFramework()
				.AddInMemoryDatabase()
				.AddDbContext<ChatLeIdentityDbContext>(options => options.UseInMemoryDatabase());
			services.AddInstance<ILoggerFactory>(new LoggerFactory());
			services.AddIdentity<ChatLeUser, IdentityRole>();
			services.AddChatLe();
			return services.BuildServiceProvider();
		}
开发者ID:IEvangelist,项目名称:chatle,代码行数:12,代码来源:TestUtils.cs

示例13: SurveyStoreTests

        public SurveyStoreTests()
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseInMemoryDatabase();
            _options = optionsBuilder.Options;

            _serviceCollection = new ServiceCollection();
            _serviceCollection
                .AddEntityFramework()
                .AddInMemoryDatabase();
            _serviceCollection.AddTransient<ApplicationDbContext>(provider => new ApplicationDbContext(provider, _options));
            _serviceCollection.AddTransient<SqlServerSurveyStore>();
        }
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:13,代码来源:SurveyStoreTests.cs

示例14: ConfigureServices

        private void ConfigureServices(ServiceCollection services)
        {
            var connectionString = Configuration["Data:DefaultConnection:ConnectionString"];
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<CoffeeStoreContext>(options =>
                {
                    options.UseSqlServer(connectionString);
                }
                );

            services.AddScoped<ICoffeeStoreContext>(provider => provider.GetService<CoffeeStoreContext>());
        }
开发者ID:dneimke,项目名称:coffeestore,代码行数:13,代码来源:Program.cs

示例15: SurveyControllerTests

        public SurveyControllerTests()
        {
            _surveyService = new Mock<ISurveyService>();
            _logger = new Mock<ILogger<SurveyController>>();
            _authorizationService = new Mock<IAuthorizationService>();

            var services = new ServiceCollection();
            services.AddEntityFramework()
                .AddInMemoryDatabase()
                .AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase());

            _target = new SurveyController(_surveyService.Object, _logger.Object, _authorizationService.Object);
        }
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:13,代码来源:SurveyControllerTests.cs


注:本文中的ServiceCollection.AddEntityFramework方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。