I have a new MVC project that I am working on that is basically a CMS that I intend to use in other projects, the structure is the following
let's say I created a new project that use the DLLs of my CMS, though a client need to add a new tables to the model based on additional criteria's, what is the best way to override the EcomerceModel.edmx so I can work with the new added table without touching the Original Dlls from the new created project.
Thank you,
Well, there is a fast solution to this, which is dividing your Data
project into several projects to keep it at minimum requirements.
So, if we take your Data
project as an example it would be separated into several class libraries something like :
For this to work probably, you will need to switch to Code First
approach, to ensure you'll have control over your migrations and models. (You don't need EF to overwrite your customizations every time you update the models, plus you'll need to keep your updates in the code and not in the database).
it's recommended to keep your models in separate assembly, to be reused in other projects without the need to reference the full data layer.
After this, in your Data.Core
you will need to reference all other Data.*
class libraries to it. Then, you can create your DbContext
like this :
public class ECommerceDbContext : DbContext
{
public DbSet<Admin> Admins { get; set; }
/// rest of Entities
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// your Entities configurations
}
}
Now, your Data
layer is set and ready to be referenced in other projects.
In your other projects that you want to reuse the current Data.Core
you need to reference Data.Core
(either by project reference or Nuget). Then, create a new class that inherits ECommerceDbContext
which would extend it. Something like :
public class ECommerceCMSContext : ECommerceDbContext
{
/// New Entities
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// New Entities configurations
}
}
Now, work with ECommerceCMSContext
to add any new table to the current context.
P.S. You can take a look at ASP.NET Core Identity
they're using similar implementation, which would be very helpful to your work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With