I have many different Model types, each with their own context. Is there a built in mechanism to get the Models context without explicitly writing a getter/helper method? I.e. is there a way to link the context with the Model itself and then have a built in method to fetch the context.
Trying to reduce the redundant code I have to write without writing something generic and unsafe, like based on class names (strings) or something.
Thanks
EDIT:
I have many classes like this:
public class CatContext : DbContext
{
public DbSet<Cat> Cat{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=animals.sqlite");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Cat>();
}
}
public class Cat
{
public string name { get; set; }
}
I want to write something generic, like:
public static void AddModel<T>(List<T> entries)
{
// TODO: Get the context for the type T
using (var context = GetModelContext<T>())
{
// do something generic, e.g. add them to the DB
}
}
But I dont want to have write a method to fetch the context in a generic way. The models aren't always related to each other either.