Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65431

Creating an Entity Key in code for a table that has a composite key

I am trying to create an Entity key for a table, in order to insert a row in the table without loading the related tables.

I am trying to use this contructor: http://msdn.microsoft.com/en-us/library/bb739024.aspx

The code example in the above link uses a different contructor.

The code I have so far is:

KeyValuePair<String, Object> key1 = new KeyValuePair<string,object>("MainId", 1);
KeyValuePair<String, Object> key2 = new KeyValuePair<string,object>("SubId", 2);


List<KeyValuePair<String, Object>> keyList = new List<KeyValuePair<String, Object>>();
keyList.Add(key1);
keyList.Add(key2);


EntityKey entitykey = new EntityKey(

How can I complete the creation of the entity key?

Upvotes: 1

Views: 554

Answers (1)

Keethanjan
Keethanjan

Reputation: 363

object value = null;
keyList.TryGetValue("MainId", out value);
EntityKey entitykey = new EntityKey("EntitySetName.TableName", "FieldName", value);

Edit: To add multiple keys:

IEnumerable<KeyValuePair<string, object>> entityKeyValues =
 new KeyValuePair<string, object>[] { new KeyValuePair<string, object>("Key", "Value") };
EntityKey key = new EntityKey("EntitySetName.TableName", entityKeyValues);

That should do the work.

Upvotes: 1

Related Questions