How can I deserialize JSON {name, value} in C#?

Database Design: What is the best way to store user config in a database?

  • OPTION1: Key/Value Pair USER_SETTINGS -Id -Code (example "Email_LimitMax") -Value (example "5") -UserId ---------------------------- OPTION2: Store configure in separate tables as required "USER_ALERT_SETTINGS" -Id -UserId -EmailAdded (i.e true) -EmailRemoved -PasswordChanged ... ... "USER_EMAIL_SETTINGS" -Id -UserId -EmailLimitMax .... ---------------------------- OPTION3: Store in a XML or JSON text in the user table "USER" -Name ... -ConfigXML

  • Answer:

    As usually, it depends :-)  It depends what you want to do with your data. I would say your option 1 is the one you should try to avoid the most, it is actually an anti-pattern (Entity-Attribute-Value anti-pattern) and it has many issues: You cannot easily make attributes mandatory You cannot enforce Data Types (the Value column must be string) or referential integrity Its hard to query everything as a "row" etc. Option 2 is actually an acceptable solution to the Option 1 EAV antipattern and is called Concrete Table Inheritance, however this comes with its own set of challenges, probably the most problematic in this case the fact that you probably want to (somewhere) show and therefore query all the users' settings, that's a lot of (outer!) joins, and what if you need to add a new setting?  New table, update all those queries etc.  This is a good alternative if you do not need to query all settings (which I am presuming you want to do) Option 3 is also acceptable and is called Serialized LOB.  It is useful if you do not have a finite limit of Settings and want to be able to define new ones at any time.  However, the problem is that you cannot easily query such a structure, it has very limited SQL support so you will need to write code that supports this. So pick your poison :-)

Mark Vilrokx at Quora Visit the source

Was this solution helpful to you?

Other answers

I would say, store in the database, forget XML for User data. "Users" are usually one of the most important data sets and it won't be long until you will need to create many other tables that link to Users. This table will receive a lot of queries, and you need to make your life easier by accessing it with stored procedures, views and so on. The things I would store in XML are those that I wouldn't query on a daily basis, not important data as Users. That being said: User table ----------- UserID SettingID (foreign key to Settings table, see below) UserDetailID - address, personal info, coffee preference etc Setting table ------------ SettingID SettingEmailID (FK to SettingEmail table) SettingAlertID SettingEmail table --------------------- SettingEmailID EmailDetail1 EmailDetail 2 SomeOtherColumnGoesHere Etc SettingAlert table ---------------------- SettingAlertID AlertDetail1 whateverforalert2 This way you have them nicely arranged, your User has a SettingID, and from there you get all the settings you require. When you figure out a new setting that you need, make a table SettingNew, and Setting table gets a SettingNewID.

Andrei Cristof

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.