Creating Array of settings names and values using ADO.NET Entities

Posted by jordan.baucke on Stack Overflow See other posts from Stack Overflow or by jordan.baucke
Published on 2011-01-02T00:00:29Z Indexed on 2011/01/02 1:54 UTC
Read the original article Hit count: 620

I'm using an ADO.NET Entities (.edmx) data-model along with MVC2 to build an application.

I have a DB table where I want to store settings for method that run elsewhere.

MVC2 allows me to create a view, editor, etc. to update this table which is great, but now when I want to do simple assignments based on column titles I'm a bit confused.

For example, I would like to easily build an array that I could offset into the record's value based on it's "Title" Column:

var entities = new ManagerEntities();
Setting[] settings = entities.settings.ToArray();

This returns something like:

Settings[0].[SettingTitle][SettingValue]

However, I would like to more easily index into the value than having to loop through all the returned settings, when they're already index.

string URL_ID_NEED = [NeededUrl][http://www.url.com]

Am I missing something relatively simple?

Thanks!


========================= *Update* =========================

Ok, I think I've got a solution, but I'm wondering why this would be so complicated, and if I'm just not thinking of the right context for ADO.NET objects, here's what I did:

public string GetSetting(string SettingName)
        {
            var entities = new LabelManagerEntities();
            IEnumerable<KeyValuePair<string, object>> entityKeyValues =
                new KeyValuePair<string, object>[] { 
                new KeyValuePair<string, object>("SettingTitle", SettingName) };
            EntityKey key = new EntityKey("LabelManagerEntities.Settings", entityKeyValues);
            // Get the object from the context or the persisted store by its key.
            Setting settingvalue = (Setting)entities.GetObjectByKey(key);
            return settingvalue.SettingValue.ToString();  
        }

This method handles the job of querying the Entities by "Key" to get back the correct value as a returned string (which I can than strip out the " ", or or cast to an integer, etc. etc.,) Am I just duplicating functionality that already exists in ADO.NET's design patterns (I'm pretty new to it) -- or is this a reasonable solution?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ADO.NET