【发布时间】:2021-11-12 07:44:19
【问题描述】:
我希望数据库中的产品显示在我的网页上(我目前有 0),我的代码是,
这是在我的 DVDDataSQL 类中:
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();
}
这是在我的 Idvddata 接口中:
public interface IDVDData
{
DVD GetProductById(int DvdId);
DVD Create(DVD Dvd);
int Commit();
DVD Delete(int DvdId);
IEnumerable<DVD> GetDVDs(string name = null);
}
这是我的数据库类:
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>();
}
ive 明确表示可以为 null 或为空,但它会抛出一个异常,它的 null ether 方式。我以前从来没有遇到过这个问题,而且我总是做这样的事情我不确定我是否错过了什么?
【问题讨论】:
-
你能显示整个代码吗?你如何初始化 scandiwebTestDbContext?
-
我添加了我连接到 db 类的所有内容
标签: c# database visual-studio exception razor-pages