I have used IdentityDbContext in my project. My database has some tables which are connected to each other (relational). I want to use Repository pattern. I declared all my Interfaces. Then, I tried to Implement them. The problem is that I cannot create an instance of IdentityAppContext, because the constructor needs an input parameter 'option'. How can I implement them?
IdentityAppContext.cs :
public class IdentityAppContext: IdentityDbContext<AppUser, AppRole, int>
{
public IdentityAppContext(DbContextOptions<IdentityAppContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbSet<AppUser> Users { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<PM> PMs { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<FileRepository> Files { get; set; }
}
IPmRepository.cs :
public interface IPmRepository
{
IEnumerable<PM> GetAllPMs();
PM GetPmById(int pmId);
bool InsertPM(PM pm);
bool UpdatePM(PM pm);
bool DeletePM(int pmId);
bool DeletePM(PM pm);
void Save();
}
PmRepository.cs :
public class PmRepository : IPmRepository
{
IdentityAppContext db = new IdentityAppContext();
public IEnumerable<PM> GetAllPMs()
{
}
public PM GetPmById(int pmId)
{
throw new NotImplementedException();
}
public bool InsertPM(PM pm)
{
throw new NotImplementedException();
}
public bool UpdatePM(PM pm)
{
throw new NotImplementedException();
}
public bool DeletePM(int pmId)
{
throw new NotImplementedException();
}
public bool DeletePM(PM pm)
{
throw new NotImplementedException();
}
public void Save()
{
throw new NotImplementedException();
}
}
GetAllPMsmethod is seldom useful - there's seldom any reason to load all rows in a table unless you want to fill a lookup table. Using a low-level "repository" interface over a high level abstraction like an ORM is actually an antipatternIdentityDbContextwith new, it's because it's meant to be used with dependency injection. Your classes that use that DbContext need to work the same way - instead of trying to manually create their dependencies, accept them as constructor parameters. That makes it easy to change the underlying storage from SQL Server to MySQL to Oracle to NoSQL databases just by changing the options registered inAddDbContext, or passed to the constructor. This allows easy unit testing - you can use the in-memory provider without modifying your DbContext or any code that uses itPmRepository, it needs a constructor that accepts anIdentityAppContextparameter.