views:

101

answers:

2

Hi I have table with this structure ID PK and two columns with FK for example ActivityID and ContactID. I try programmaticlly insert some value in this two FK columns. How can I do this, any help is appriciated.Tnx

A: 

I think the best solution would be to get rid of primary key, set up combination of ActivityID and ContactID as PK and then recreate whole model in visual designer. Every Activity object will have Contacts navigation property and every Contact will have Activities. You will be able to add contacts to activity by calling:

activity.Contacts.Add(contact);

If you really need additional ID, it will be more complicated.

LukLed
I have this two FK in datatable and i try to insert this in this rel.table on sql.
ziks
A: 

If you want to use your structure - you have to get instances of Activity and of Contact and just set corresponding properties on new entity.

var newActivityContact = new ActivityContact();// m_Entities.CreateActivityContact(0);
newActivityContact.Activity = activityRepository.GetById(activityId);
newActivityContact.Contact = contactRepository.GetById(contactId);
m_Entities.AddToActivityContact(newActivityContact);
m_Entities.SaveChanges();
mmcteam.com.ua
activityRepository.GetById(activityId) Is this query to get this activity.
ziks
yes - retrieve it from your m_Entities instance like m_Entities.Contact.FirstOrDefault(c=>c.Contact_Id == contactId);
mmcteam.com.ua