我想在我的網頁上顯示我在資料庫中的產品(我目前有0個),我的代碼是,
這是在我的DVDDataSQL類中。
這是在我的DVDDataSQL類中:
publicIEnumerable<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()。
}
這是在我的Idvddata介面中 :
public interface IDVDData
{
DVD GetProductById(int DvdId)。
DVD Create(DVD Dvd) 。
int Commit() 。
DVD Delete(int DvdId) ;
IEnumerable<DVD> GetDVDs(string name = null) ;
}
而這是我的db類:
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; }
我在啟動程式中加入了這個內容
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>();
}
我清楚地指出,它可以是空的或空的,但它拋出一個例外,它的空的方式。我從來沒有遇到過這個問題,我總是做這樣的東西,我不知道我是否錯過了什么?
uj5u.com熱心網友回復:
你必須注入你的dbcontext,像這樣的東西
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.Function.Like(r.Name, param)).ToList()。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/319996.html
標籤:
