I want the products that I have in database to display on my web page (i currenly have 0) and my code is,
This is in my DVDDataSQL class:
public IEnumerable<DVD> GetDVDs(string name = null)
{
var param = !string.IsNullOrEmpty(name) ? $"{name}%" : name;
return scandiwebTestDbContext.DVDs.Where(r => string.IsNullOrEmpty(name) || EF.Functions.Like(r.Name, param)).ToList();
}
This is in my Idvddata interface :
public interface IDVDData
{
DVD GetProductById(int DvdId);
DVD Create(DVD Dvd);
int Commit();
DVD Delete(int DvdId);
IEnumerable<DVD> GetDVDs(string name = null);
}
and this is my db class:
public class ScandiwebTestDbContext : DbContext
{
public ScandiwebTestDbContext(DbContextOptions<ScandiwebTestDbContext> options) : base(options)
{
}
public DbSet<DVD> DVDs { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Furniture> Furnitures {get;set;}
}
i have this in the startup program added
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc();
services.AddDbContext<ScandiwebTestDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ScandiDb")));
services.AddScoped<IDVDData, DVDDataSQL>();
services.AddScoped<IBookData, BookDataSQL>();
services.AddScoped<IFurnitureData, FurnitureDataSQL>();
}
ive clearly stated that its okay to be null or empty but it throws an exception that its null ether way. ive never had this problem before and ive always done this stuff like this im not sure if im missing something?
CodePudding user response:
you have to inject your dbcontext, something like this
public class DVDData : IDVDData
{
private readonly ScandiwebTestDbContext _scandiwebTestDbContext
public DVDData (ScandiwebTestDbContext scandiwebTestDbContext)
{
_scandiwebTestDbContext=scandiwebTestDbContext;
}
.....
public IEnumerable<DVD> GetDVDs(string name = null)
{
var param = !string.IsNullOrEmpty(name) ? $"{name}%" : name;
return _scandiwebTestDbContext.DVDs.Where(r => string.IsNullOrEmpty(name) || EF.Functions.Like(r.Name, param)).ToList();
}
}