Skip to main content

Posts

Showing posts with the label EF Core

Fluent mapping approach for EF Core

With the first release of EF Core, there wasn't any proper approach of fluent mapping of entities, in an easy manner for enterprises application where a lot of entities needs to be involved. You might have ended up with nasty or repeated codes. At that time only way was to compose mapping based on ModelBuilder and by using inline fluent mapping codes. Like in newer version EF Core 2.0 there had been a new release of  IEntityTypeConfiguration for the creation of mapping based on single entity and could be registered with  ApplyConfiguration  by passing a new instance of  IEntityTypeConfiguration object.   https://docs.microsoft.com/en-us/ef/core/what-is-new (Self-contained type configuration for code first) This is really good for big application as we can separate mappings based on each entity. In this article, we would see how to achieve similar functionality in EF Core 1.0 and this implementation is a bit cleaner than 2.0 built-in classes. So, in ...

Data seed through JSON files using EF or any ORM

Last time I had demonstrated a concept for Data Seed through any ORM or MongoDB . The idea is to feed the data to DB at first run or based on condition. The advantage is no one has to manage DB manually with SQL script which feels more natural with Code First approach and every developer can have their own copy with this approach. I am going to extend the same idea by importing data from JSON file. There are few reasons to do through JSON file as we do not want to keep increasing our codes for data import which may end up with   Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals . Also in the case of a huge number of records it does not make sense and with newer development practice JSON feels more natural for data. First please go to  Data seed for the application with EF, MongoDB or any other ORM  as I would be extending same items to allow it through JSON file. It is always a great idea to have a bas...

Data seed for the application with EF, MongoDB or any other ORM.

Most of ORMs has moved to Code first approach where everything is derived/initialized from codes rather than DB side. In this situation, it is better to set data through codes only. We would be looking through simple technique where we would be Seeding data through Codes. I would be using UnitOfWork and Repository pattern for implementing Data Seeding technique. This can be applied to any data source MongoDB, EF, or any other ORM or DB. Things we would be doing. - Creating a base class for easy usage. - Interface for Seed function for any future enhancements. - Individual seed classes. - Configuration to call all seeds. - AspNet core configuration to Seed data through Seed configuration. Creating a base class for easy usage public abstract class BaseSeed<TModel> where TModel : class { protected readonly IMyProjectUnitOfWork MyProjectUnitOfWork; public BaseSeed(IMyProjectUnitOfWork MyProjectUnitOfWork) { ...