EF programmatically embed many of the many

I have a table with this PK ID structure and two columns with FK like ActivityID and ContactID. I am trying to programmatically insert some value into these two FK columns. How can I do this, any help would be appriciated.Tnx

+1


a source to share


2 answers


If you want to use your own structure, you need to get instances of Activity and Contact and just set the appropriate properties for the new object.



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();

      

0


a source


I think the best solution would be to get rid of the primary key, set up the combination of ActivityID and ContactID as PK, and then recreate the whole model in visual designer. Every object Activity

has a navigation property Contacts

, and everyone Contact

will Activities

. You will be able to add contacts to the action by calling:

activity.Contacts.Add(contact);

      



If you really need an additional ID, this will be trickier.

0


a source







All Articles