In the EFStoreRepository class, I have a variable of StoreDbContext type(context)
StoreDbContext class and Products field inside it are not static. but I can call Products without instantiating the StoreDbContext class like a static field.
How do we access a non-static field without the need for instantiation?
using Microsoft.AspNetCore.Mvc;
using SportsStore.Models;
namespace SportsStore.Controllers
{
public class HomeController : Controller
{
private IStoreRepository repository;
public HomeController(IStoreRepository repo)
{
repository = repo;
}
public IActionResult Index()
{
return View(repository.Products);
}
}
}
using Microsoft.EntityFrameworkCore;
namespace SportsStore.Models
{
public static class SeedData
{
public static void EnsurePopulated(IApplicationBuilder app)
{
StoreDbContext context = app.ApplicationServices
.CreateScope().ServiceProvider.GetRequiredService<StoreDbContext>();
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}
if (!context.Products.Any())
{
context.Products.AddRange(
new Product
{
Name = "Kayak",
Description = "A boat for one person",
Category = "Watersports",
Price = 275
},
new Product
{
Name = "Lifejacket",
Description = "Protective and fashionable",
Category = "Watersports",
Price = 48.95m
},
new Product
{
Name = "Soccer Ball",
Description = "FIFA-approved size and weight",
Category = "Soccer",
Price = 19.50m
},
new Product
{
Name = "Corner Flags",
Description = "Give your playing field a professional touch",
Category = "Soccer",
Price = 34.95m
},
new Product
{
Name = "Stadium",
Description = "Flat-packed 35,000-seat stadium",
Category = "Soccer",
Price = 79500
},
new Product
{
Name = "Thinking Cap",
Description = "Improve brain efficiency by 75%",
Category = "Chess",
Price = 16
},
new Product
{
Name = "Unsteady Chair",
Description = "Secretly give your opponent a disadvantage",
Category = "Chess",
Price = 29.95m
},
new Product
{
Name = "Human Chess Board",
Description = "A fun game for the family",
Category = "Chess",
Price = 75
},
new Product
{
Name = "Bling-Bling King",
Description = "Gold-plated, diamond-studded King",
Category = "Chess",
Price = 1200
}
);
context.SaveChanges();
}
}
}
}
using SportsStore.Models;
using Microsoft.EntityFrameworkCore;
internal class Program
{
private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<StoreDbContext>(opts =>
{
opts.UseSqlServer(builder.Configuration["ConnectionStrings:SportsStoreConnection"]);
});
builder.Services.AddScoped<IStoreRepository, EFStoreRepository>();
var app = builder.Build();
app.UseStaticFiles();
app.MapDefaultControllerRoute();
SeedData.EnsurePopulated(app);
app.Run();
}
}


using Microsoft.EntityFrameworkCore;
namespace SportsStore.Models
{
public class StoreDbContext : DbContext
{
public StoreDbContext(DbContextOptions<StoreDbContext> options) : base(options) { }
public DbSet<Product> Products => Set<Product>();
}
}
using System.ComponentModel.DataAnnotations.Schema;
namespace SportsStore.Models
{
public class Product
{
public long? ProductID { get; set; }
public string Name { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
[Column(TypeName = "decimal(8,2)")]
public decimal Price { get; set; }
public string Category { get; set; } = String.Empty;
}
}
namespace SportsStore.Models
{
public interface IStoreRepository
{
IQueryable<Product> Products { get; }
}
}
namespace SportsStore.Models
{
public class EFStoreRepository : IStoreRepository
{
private StoreDbContext context;
public EFStoreRepository(StoreDbContext ctx) => context = ctx;
public IQueryable<Product> Products => context. Products;
}
}