Entity Framework and NHibernate are Object-relational mapping frameworks (ORM) and are used to map database schema to domain objects in any object-oriented language.
Table of Content
- 1. What is Entity Framework?
- 2. What is NHibernate?
- 3. Version History
- 4. Platforms
- 5. Supported database
- 6. Table inheritance
- 7. Primary key generation
- 8 Querying and modification
- 9. Migration/Database generation
- 10. Lazy and Explicit loading
- 11. Conclusion
In this blog, we will see the differences and similarities between Entity Framework and NHibernate.
What is Entity Framework?
Entity Framework is an open-source and object-relational mapping (ORM) framework and used to include business logic in the application. Entity framework developed by Microsoft and first released in 2008 and no source code was available to be used for free because of license but now it is free to be used. Entity Framework is similar to another object-relational framework and provides mechanisms for data storage and data access from the database.
Entity framework has the following features like tracking, identity relations, lazy loading, query translation so that developers need not focus on data access fundamentals.
What is NHibernate?
Nhibernate is also free and open-source object-relational mapping (ORM) architecture and provides similar objectives to object-relational mapping (ORM) architecture.
NHibernate maps .Net classes to a database table and provide database query and retrievals facilities to the Asp.Net project. Object-relational mapping tools us used to map object properties to the column of the table in the database.
Version 1 -> Version 1 was released with .Net 3.5 service pack1 and provide support for model-first and database first workflow.
Version 4 ->Version 4 was released with .Net 4 and provide support for lazy loading, self-tracking, Plain object CLR object (POCOs), and generate Text template transformation toolkit (T4).
Version 4.1 and 4.3 ->Version 4.1 and 4.3 provides support for the code first development model with cleaner API and also provide migration through NuGet 4.3.
Version 5 ->Version 5 provides support for enumerated type, spatial type, table-valued function.
Version 6 ->Version 6 provide support for interceptor, logging, asynchronous operation, custom conventions, store procedure for CRUD operations.
.Net core 1 -> .Net core 1 provide basic functionalities and provide support for non-relational database and add shadow property.
.Net core 1.1 ->.Net core 1.1 provide support for mapping fields, explicit loading, connection resiliency, and APIs.
.Net Core 2 ->.Net core 2 provide support for operator, DbContext, pooling, explicitly complied queries, scalar function mappings, and self-contained entity configuration, classes.
Version 1 -> Version 1 was original and didn’t support generics
Version 2 -> Version 2 dropped support for .Net 1.x.
Version 3 -> Version 3 provides support for LINQ, QueryOver, and Lazy loading properties.
Version 3.2 and 3.3 ->Version 3.2 and 3.3 support fluent configuration and conventions, mapping of views, HQL (Hibernate query language), and integrated byte code generator.
Version 4 -> Version 4 target .Net 4 and BCL (Base class library).
Version 5 ->Version 5 provides support for asynchronous programming, TransactionScope with cleaning with removing absolute code.
Version 5.1 -> Version 5 provide support for .Net core.
Entity Framework support any platform because Entity framework runs on .Net core and it is based on a provider model that theoretically can work with any database except relational.
NHibernate support .Net 4 and mono which is available in higher running on windows or platform and version 5.1 support .Net core. .Nhibernate supports relational databases and no plan to support non-relational databases.
NHibernate is a single DLL that is distributed through NuGet with no other dependency.NHibernate required order sets where lesi. Collections are also needed. In NHibernate, everything in built-in needs the full .NET framework or .NET core.
On other hand, the Entity framework has multiple DLL in NuGet packages. Entity framework provides dependencies are brought along as needed.
Entity framework builds on .Net core and provided features like logging and dependency injection and its internal components.
NHibernate doesn't use dependency injection therefore a way of replacing service is quite different from service to service. NHibernate is not extensible as an entity framework.
NHibernate uses a configuration object and it produces a session factory. The session is produced by the session factory which are lightweight abstractions encapsulating an ADO.Net connection.
Entity framework use DbContect which includes mapping and configuration information and exposes all APIs and communicates with the database. It is a simple model but separation of concerns is efficient in NHibernate.
Entity framework supports SQL Server, SQLite, In-memory. In-memory is used for unit testing. Entity framework works on Cosmos DB and Oracle Database.
NHibernate support SQL Server, SQL Server CE, Oracle Database, Inges, PostgresSQL, MySQL, DB2, Sybase, Informix, SQLite, Firebird, ODBC(open-source connectivity), or OLE (Object Linking and Embedding)DB.
Configuration and mapping
Entity framework used code-based (fluent) configuration or attribute-based (fluent) mapping.
NHibernate uses XML and Fluent configuration and mapping. NHibernate Attributes is used to provide attribute mapping. NHibernate provides custom conventions and EF doesn't provide them.
Both of them need mapping and both can map non-public members, properties, and fields and both provide a notion of value types. Shadow property (Entities that are part of DB schema, but not mapped in the class model) is supported by both.
NHibernate provides Single table inheritance, Class table inheritance, Concrete table inheritance.
Entity framework supports single table inheritance /Table per class hierarchy.
Primary key generation
EF support auto-generated IDENTITY key on SQL Server and SQLite, sequence on SQL server, and manually assigned values.
NHibernate provides identity on SQL server/Sybase and auto-increment value in other databases, sequence, database-independent high-low algorithm, High-low based on sequence, flavors of GUIDs (Globally unique identifiers).
Looking for the Best ASP.Net Web Development Company ?
Your Search ends here.
Querying and modification
EF core use LINQ and SQL for query and doesn’t provide string type insert, update and delete and doesn’t provide support for grouping and no projection to non-entity types.
NHibernate provides LINQ, Criteria API, HQL, SQL, QueryOver, and all support pagination.
NHibernate support store procedure which is not supported by EF. In NHibernate, multiple queries can be queued, executed, and retrieved at the same time. Asynchronous query and modification are supported by both.
Migration API is used by EF and NHibernate to generate database and update schema – automatic or based on request.
Lazy and Explicit loading
The current version of EF doesn't support lazy loading, only explicit or eager loading of associated entities and collection support.
NHibernate support lazy loading of associated entities, collection, and even single properties. NHibernate supports eager loading and explicit loading.
In this blog, we have discussed similarities and differences between Entity Framework and NHibernate. Entity Framework and NHibernate are Object-relational mapping frameworks (ORM) and are used to map database schema to domain objects in any object-oriented language.