2
votes

is it possible to create an EF model on the base of a dynamic structure? F.e. i have a Customer, which can have custom fields defined by the administrator of the system. Based on that the customer related custom fields table, is being altered and a new column is added for the new defined field.

Is there any possibility to implement this in the EF model or should i just work without it and make an own implementation to maintain data?

Thanks!

EDIT1: So after checking out the EAV Model i came to the conclusion that it does not quite suit my needs, if i understood it right, since my fields are not dynamic per entity, but the same for all entities. So basically my table structure is like this:

Customer { Id:int, Name:string }

Customer_CustomFields { Id:int, CustomerId:int, UserDefinedName1, UserDefinedName2, ... }

The only thing varying here is the UserDefinedName-Column. The name of the column is set to the name the user defined for the custom field f.e. "CF_RandomInfo". The type of this column changes in dependance of which type the user selected for this custom field (nvarchar(max), float, etc). Also the number of user defined columns is unknown.

I was wondering if i can implement this structure as Contained OpenType into the customer class?

1

1 Answers

1
votes

You can make use of the EAV model link.

But! if you can make assumption like 'no more than XX string properties', you can reserve this properties in model like StringProperty1, StringProperty2... (Microsoft often use this method for user defined properties) and enable/rename them on EDM level of your data service.

edit:

OData v4 supports opentypes link.

But you still have to store your dynamic properties in database EAV is most convenient way in SQL bases - you just have to store allowed properties and types per table/Entity type.

If you want to use OpenType feature of odata, you have to provide properties dictionary on your own(check example from link). So you need to join your "base" entity with expanded properties and SELECT them into dictionary.

Predefined properties way:

-column way doesn't change, you just change name in EDM or on EF level via fluentAPI

-String + Decimal + odata casting + validation covers pretty much every type you need