// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, SportsDbContext context) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } context .SeedRoles() .SeedSportTypes() .SaveChanges(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, SportsDbContext sportsDbContext, SeedDatabase seedDatabase) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); sportsDbContext.Database.Migrate(); seedDatabase.Seed().GetAwaiter().GetResult(); }
public async Task Entity_InMemory_BreakForeignKey() { var options = new DbContextOptionsBuilder <SportsDbContext>() .UseInMemoryDatabase(databaseName: "test1") .Options; using (var context = new SportsDbContext(options)) { context.Leagues.Add(LeagueFactory.NewLeague(1)); context.Leagues.Add(LeagueFactory.NewLeague(2)); context.SaveChanges(); } using (var context = new SportsDbContext(options)) { var result = await context.Leagues .Include(x => x.Conferences) .Include(x => x.Divisions) .Include(x => x.Teams) .Include(x => x.Seasons).ThenInclude(x => x.TeamMaps).ToListAsync(); //should be able to get the team maps with the navigation properties Console.WriteLine(result[0].Seasons[0].TeamMaps[0]); } }
static void AddArticle(string authorName, string title, string content) { using (SportsDbContext db = new SportsDbContext()) { int teamPageId = ( from au in db.Author join tp in db.TeamPage on au.AuthorId equals tp.AuthorId where au.AuthorName == authorName select tp.TeamPageId ) .Single(); Article newArticle = new Article { TeamPageId = teamPageId, Title = title, Content = content }; db.Article.Add(newArticle); db.SaveChanges(); } Console.WriteLine($"Added a new article by: '{authorName}'"); }
static void AddTeamPage(string username, string teamName) { using (SportsDbContext db = new SportsDbContext()) { int authorId = ( from au in db.Author where au.Username == username select au.AuthorId ) .Single(); TeamPage newTeamPage = new TeamPage { AuthorId = authorId, TeamName = teamName }; db.TeamPage.Add(newTeamPage); db.SaveChanges(); } Console.WriteLine($"Added Team: '{teamName}' which will feature articles from: '{username}'"); }
public List <Article> RetrieveAll() { using (var db = new SportsDbContext()) { return(db.Article.ToList()); } }
public void Update(int articleId, string title, string content) { using (var db = new SportsDbContext()) { SelectedArticle = db.Article.Where(a => a.ArticleId == articleId).FirstOrDefault(); SelectedArticle.Title = title; SelectedArticle.Content = content; // write changes to database db.SaveChanges(); } }
public void Create(int articleId, string title, string content, string teamPageId) { var newArt = new Article() { ArticleId = articleId, Title = title, Content = content }; using (var db = new SportsDbContext()) { db.Article.Add(newArt); db.SaveChanges(); } }
public static SportsDbContext SeedRoles(this SportsDbContext context) { foreach (string name in Enum.GetNames(typeof(RoleType))) { if (!context.Roles.Any(x => x.Name == name)) { context.Roles.Add(new Role { Name = name }); } } return(context); }
public static SportsDbContext SeedSportTypes(this SportsDbContext context) { foreach (string sportType in sportTypes) { if (!context.SportTypes.Any(x => x.Name == sportType)) { context.SportTypes.Add(new SportType { Name = sportType }); } } return(context); }
public SportsData(SportsDbContext dbContext, UserManager <User> userManager, RoleManager <Role> roleManager, SignInManager <User> signInManager) { Context = dbContext; UserManager = userManager; RoleManager = roleManager; SignInManager = signInManager; _eventService = _eventService.InitLazy(new EventService(this)); _userService = _userService.InitLazy(new UserService(this)); _sportTypeService = _sportTypeService.InitLazy(new SportTypeService(this)); _gameRoomService = _gameRoomService.InitLazy(new GameRoomService(this)); _matchService = _matchService.InitLazy(new MatchService(this)); _moderationManager = _moderationManager.InitLazy(new ModerationManager(this)); }
static void AddAuthor(string username, string email, string password, string authorName) { using (SportsDbContext db = new SportsDbContext()) { Author newAuthor = new Author { Username = username, Email = email, Password = password, AuthorName = authorName }; db.Author.Add(newAuthor); db.SaveChanges(); } Console.WriteLine($"Added User '{username}'"); }
public async Task Entity_SqlLite_ForeignKey() { var options = new DbContextOptionsBuilder <SportsDbContext>() .UseInMemoryDatabase(databaseName: "test2") .Options; using (var context = new SportsDbContext(options)) { context.Leagues.Add(LeagueFactory.NewLeague(1)); context.SaveChanges(); } //change the team using (var context = new SportsDbContext(options)) { var league = await context.Leagues.Include(x => x.Teams).FirstAsync(); context.Update(league); league.Teams.First().Name = "abc"; league.Teams.Add(new TeamDao() { Name = "zz" }); context.SaveChanges(); var league2 = await context.Leagues.Include(x => x.Teams).FirstAsync(); //league.Teams.Firs //var result = await context.Leagues // .Include(x => x.Conferences) // .Include(x => x.Divisions) // .Include(x => x.Teams) // .Include(x => x.Seasons).ThenInclude(x => x.TeamMaps).ToListAsync(); //should be able to get the team maps with the navigation properties Console.WriteLine("done"); } }
public SportsApplicationController(SportsDbContext context) { this._context = context; }
public HomeController(SportsDbContext context) { _context = context; }
public TestRepository(SportsDbContext context) { this.context = context; }
public UserRepository(SportsDbContext dbContext) { _dbContext = dbContext; }
public UsersController(SportsDbContext context) { _context = context; }
public TestRepository(SportsDbContext dbContext) { _dbContext = dbContext; }
public SeedDatabase(SportsDbContext dbContext) { _dbContext = dbContext; }
public OrderRepository(SportsDbContext db) { this.db = db; }
public TestController(SportsDbContext context) { this.context = context; }
public UserRepository(SportsDbContext context) { this.context = context; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SportsDbContext context) { loggerFactory.AddConsole(Configuration.GetSection("logging")); loggerFactory.AddDebug(); app.UseApplicationInsightsRequestTelemetry(); app.UseApplicationInsightsExceptionTelemetry(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error!"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); DBInitializer.Initialize(context); }
public static void EnsurePopulated(IApplicationBuilder app) { SportsDbContext context = app.ApplicationServices .CreateScope().ServiceProvider.GetRequiredService <SportsDbContext>(); if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } if (!context.Details.Any()) { context.Details.AddRange( new Details { Name = "Kayak", Description = "A boat for one person", Category = "Watersports", Price = 275 }, new Details { Name = "Lifejacket", Description = "Protective and fashionable", Category = "Watersports", Price = 48.95m }, new Details { Name = "Soccer Ball", Description = "FIFA-approved size and weight", Category = "Soccer", Price = 19.50m }, new Details { Name = "Corner Flags", Description = "Give your playing field a professional touch", Category = "Soccer", Price = 34.95m }, new Details { Name = "Stadium", Description = "Flat-packed 35,000-seat stadium", Category = "Soccer", Price = 79500 }, new Details { Name = "Thinking Cap", Description = "Improve brain efficiency by 75%", Category = "Chess", Price = 16 }, new Details { Name = "Unsteady Chair", Description = "Secretly give your opponent a disadvantage", Category = "Chess", Price = 29.95m }, new Details { Name = "Human Chess Board", Description = "A fun game for the family", Category = "Chess", Price = 75 }, new Details { Name = "Bling-Bling King", Description = "Gold-plated, diamond-studded King", Category = "Chess", Price = 1200 } ); context.SaveChanges(); } }
public UnitOfWork(SportsDbContext context) { this.context = context; }
public UserController(SportsDbContext context) { this.context = context; }
public TestController(SportsDbContext context) { uof = new UnitOfWork(context); }