Last week I had the pleasure to work with a team that started using Entity Framework Core for the first time. They had a lot of experience using NHibernate, so the concept of an ORM was not new. But it was interesting to see which things are obvious when switching to EF Core and which are not.
The start was a little bit of trial and error, but the next time they contacted me wasn’t to solve the next problem. Progress! Instead they had a question…
In Nhibernate the mapping between your classes and database tables is typically done through HBM files(if you like XML) or through code.
In EF Core you can override the OnModelCreating
method on the DbContext
:
Using separate files for your mapping code
As the team was used to have separate files for the mapping code, they were wondering if this was possible with EF Core as well.
Luckily, the answer is yes. To do that you need to implement the IEntityTypeConfiguration
interface:
and change the OnModelCreating
implementation to search for mapping files in a specific assembly(or explicitly add them):