R – Can nhibernate/orm support eav

entity-attribute-valuenhibernateorm

I've recently inherited a system that relies heavily on an EAV database structure, and is really struggling from a performance perspective.

What I want to do is use nhibernate or another suitable ORM product to map these EAV tables to entities in such a way that we can map a row to a property. We can then refactor the database in order to make it relational. Does anyone know if this is possible? An example would also be appreciated! 🙂

To give you a feel for the structure, it looks something like this:

Entity (EntityId)
EntityVarchar (EntityId, VarcharValue)
EntityFloat (EntityId, VarcharValue)

and so on. If I had a Customer entity I'd like to say Customer.Name to get the name rather than Customer.Varchar["Name"].

Please note there is no need in our system to use an EAV model, we don't allow runtime changes to data structures, and I believe it's a bad practice anyway.

Best Answer

I don't think this will be that easy. Though there are generators able to read a database schema and generate appropriate mapping and classes this would only result in an access layer for your existing eav db. To generate a relational version of this db you then would have to read the data and create new domain objects containing properties for the the values set in your db. This could most likely result in one big table containing all the properties exist in the eav db. Therefor I think a better approach would be to create a relational DB model by hand analysing the existing data and considering the needs of your application. Especially Table inheritance should be your friend here. Then after you have created access layers for both schemas you still would have to write a mapping for data migration.

Related Topic