Sachin Singh

Sachin Singh

  • 7
  • 55.8k
  • 81.8k

EF add data to the same navigation property many to many .

Aug 5 2020 8:35 AM
I have a post table and a tag table and bridge table PostTag.
 
Post 1 has 23 tags , meaning
  post p= context.posts.where(p=>p.id=12);
 p.tags.Count() =12;
 
Ok ,fine.
Now I added some new tags to tag table but i want to attach these tags to the same old post
 
so what i did,

  1. Post p= context.posts.where(p=.p.id=12);  
  2. foreach(tag t in tags)  
  3. {   
  4. p.tags.add(t);  
  5. }  
  6. context.savechanges(); 
 
There is no error, but no output aswell , meaning still post has old tags and new tags are not added to the post.

Answers (9)