I have an Entity Framework DB Context file.
I am trying to setup a Moq framework in NUnit. Currently receiving error below for Moq Nunit test. How would I setup the DBContext, and add items to a Product Table?
"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext."
Electronics DB Context file
public partial class ElectronicsContext : DbContext
{
public ElectronicsContext()
{
}
public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
: base(options)
{
}
public virtual DbSet<Product> Product { get; set; }
public virtual DbSet<ProductCategory> ProductCategory { get; set; }
Startup.cs
var connection = @"Server=localhost;Database=Electronics;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<ElectronicsContext>(options => options.UseSqlServer(connection));
Moq Nunit Test
[SetUp]
public void Setup()
{
var ElectronicsContext = new Mock<ElectronicsContext>();
var ProductRepository = new Mock<ProductRepository>();
Product producttest = new Product();
_dbContext.Product.Add(new Product {ProductId = 1, ProductName = "TV", ProductDescription = "TV testing",ImageLocation = "test"});
_dbContext.SaveChanges();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…