'EntityFramework Core dbContext doesn't recognize SaveChanges() [closed]
I am working on .NET CORE 6 App along with Entity Framework CORE. I have created DbContext class that I am reference in my abstract BaseCommand Class. The script to create record inherited from BaseCommand class and hence got reference to dbContext. I am not able to recognise SaveChanges and need help based on current implementation
DbContext
public class AppDbContext : DbContext, IAppDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options) { }
public virtual DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
}
}
BaseCommand
public abstract class BaseCommand<TResult>
{
protected IAppDbContext db { get; }
protected BaseCommand(IAppDbContext dbContext)
{
this.db = dbContext;
}
public abstract TResult Execute();
}
Create Customer Class
using Microsoft.EntityFrameworkCore;
public class CreateCustomerCommand : BaseCommand<Customer>, ICreateCustomerCommand
{
public CreateCustomerCommand(IAppDbContext dbContext)
: base(dbContext)
{
}
public CustomerDto CustomerDtoObject { get; set; }
public override Customer Execute()
{
if(CustomerDtoObject != null)
{
var customer = new Customer
{
Name = CustomerDtoObject.Name,
Email = CustomerDtoObject.Email,
Address = CustomerDtoObject.Address,
};
db.Customers.Add(customer);
db.SaveChanges(); // not recognised need help here?
}
}
}
IAppDbContext
public interface IAppDbContext
{
int SaveChanges();
DatabaseFacade Database { get; }
DbSet<Customer> Customers { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|