Can EntityFramework support an EAV model? Is this a workable scenario, or a nightmare? I want to use an EAV model for a system, and I'd like to embrace EF if possible, but I'm concerned that these two philosophies are in conflict.
相关问题
- Entity Framework Code First Migration
- Entity Framework throws exception - Network Relate
- Slow loading first page - ASP.NET MVC
- query and create objects with a one to many relati
- Entity Framework override default property convent
相关文章
-
EF6 DbSet
returns null in Moq - Entity Framework 4.3.1 failing to create (/open) a
- EF6 code first: How to load DbCompiledModel from E
- Why do I need a ToList() to avoid disposed context
- Code-First Add-Migration keeps adding the same col
- EF Codefirst validate unique property?
- EF Core 'another instance is already being tra
- Add XML documentation / comments to properties/fie
It depends how do you expect to use EAV in the application. EF can be used to map this:
This is what can be persisted and what can be queried by Linq-to-entities. Now you can make your entity usable by defining helper properties (can be used only in your application but not by persistance or querying). These helper properties can be used only for well known attributes which will always exists for entity type - optional attributes must be still accessed in collection:
This is not very nice because of conversions and collection searching. If you access data multiple times they will be executed multiple times.
I didn't tried it but I think this can be avoided by implementing a similar interface by each entity:
Now you will handle
ObjectMaterialized
andSavingChanges
events onObjectContext
. In the first handler you will executeInitialize
if materialized object implementsIEavEntity
in the second handler you will iterateObjectStateManager
to get all updated or inserted entities implementingIEavEntity
and you will executeFinalize
. Something like: