'Move Entity Framework entities configurations to separate class per Db
I use code-first approach and have used SQL Server as database. All the entities are configured like below:
public interface IBaseEntityTypeConfiguration<T>
{
public void Map(EntityTypeBuilder<T> builder);
}
// My entity
public class Student : IBaseEntityTypeConfiguration<Student>
{
public Id { get; set; }
public string Name { get; set; }
public void Map(EntityTypeBuilder<Student> builder)
{
// Configure entity
}
}
In the ApplicationDbContext all the classes which are inherited from IBaseEntityTypeConfiguration are added to the modelBuilder.Configurations.
But I came up with a new requirement. We should switch from SQL Server to PostgreSql!
I don't want to change the Map() method in all entities, but I want to add some kind of provider which says to modelBuilder that use configurations for SQL Server configurations or PostgreSql configs.
Something like below:
public class ApplicationContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// var entities = new MsSqlEntityConfiguration().GetAllTypes();
var entities = new PostgreEntityConfiguration().GetAllTypes();
foreach(entity in entities)
modelBuilder.Configuration.Add(entity); // As an example
}
}
Is there any solution that exists as described?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
