开发者

EF programmatically insert many to many

I have table with this structure ID PK and two colum开发者_运维知识库ns with FK for example ActivityID and ContactID. I try programmatically insert some value in this two FK columns. How can I do this, any help is appriciated.Tnx


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜