Click here to Skip to main content
15,881,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello. I'm working on MVC application and I want to save Dictionary<string,> into DB I already have (generated with EF, DB first). I've didn't manage to find solutions, apparently EF doesn't support dictionary.
I will use this to associate different hashing and cryptic algorithms in my app. Here is sample code that illustrates what i'd like to save in db.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {

        public delegate String AlgDelegte(String a, String b);

        public static String AlgFunc(String a, String b) {
            return String.Concat(a, b);
        }

        public static String Alg1Func(String a, String b) {
            return String.Concat(a, "\nBLA\n", b);
        }
        static void Main(string[] args) {
            AlgDelegte alg = new AlgDelegte(AlgFunc);
            AlgDelegte alg1 = new AlgDelegte(Alg1Func);
            Dictionary<String, AlgDelegte> algorithms = new Dictionary<string, AlgDelegte>();

            algorithms.Add("zeroth", alg);
            algorithms.Add("first", alg1);

            Console.WriteLine(algorithms["first"]("a", "b"));

        }
    }
}


Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Aug-14 18:13pm    
Not clear what you are trying to achieve with this code sample. Storing and retrieving dictionary content using relational database is quite simple; please see my answer.
—SA
Paulo Zemek 8-Aug-14 19:41pm    
I think he wants to save an entire dictionary as a single field in the database... like a string or int.

The concept of dictionary if very close to relational model. You just need to have a table of key-value pairs, where a dictionary key could be a primary table key. If you have to store several different dictionaries in a single table, the key uniqueness can be violated. In this case, use a compound key: one columns identifying a dictionary instance, another one — a dictionary key. Please see:
http://en.wikipedia.org/wiki/Compound_key[^].

—SA
 
Share this answer
 
Thanks, this helps. But is it possible to save these function delegates as value in table?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900