I use SQL Server and Entity Framework as ORM.
Currently I have a table Product which contains all products of any kind. The different kinds of products possess different attributes.
For example:
- All products of kind
TVhave attributestitle,resolutionandcontrast - Where as all products of kind
Carhave attributes likemodelandhorsepower
Based on this scenario I created a table called Attribute which contains all the attributes of a product.
Now to fetch a product from database I always have to join all the attributes.
To insert a product I have to insert all the attributes one by one as single rows.
The application is not just a shop or anything like it. It should be possible to add/remove an attribute to/from a kind of product on the fly without changing the db.
But my questions to you is still:
- Is this a bad design?
- Is there another way of doing it?
- Will my solution slow down significant? (f.e. an insert takes several seconds assumed the product has hundreds of attributes...)
Update
The problem is that my application is really complex. There are a lot of huge algorithms. The software is used for statistical purposes.
One problem for example is the following one: In an algorithm-table I'm storing which attributes are used for filters. Say an administrator wants to filter all cars that have less than 100 horsepowers. The filters are dynamical, what means that I have a filter table which stores the filter type (lessThan) and the attribute (horsepowers). How can I keep this flexibility with the suggested approaches (with "hardcoded" columns)?
BaseProducttable with the common attributes, and thenCarsandTVtables that reference the base products table, and add the additional attributes; or if that's still too rigid: (2) put the base attributes on the table, and store any additional custom attributes into anXMLcolumn - those are just the two alternatives that I've personally used with great success - I'm sure there are plenty more!Carentity in EF that has some of its values inBaseProductand the car-specific values inCar. Same forTV. That way, if you can move your calculations from SQL to EF, you could simply use a nice object model and don't worry about JOINs and everything ...