c# - Entity frame work Saving Many to Many Relationship -


i have news entity , define constraint news based on groupid using groupnews table. when add new news because nothing added groupnews table , news doesn't show in web page. need add newsid , groupid groupnews table.

enter image description here

i defined groupnews in mywebcontect.cs :

public class groupnews : dbcontext {     public dbset<group> group { get; set; }     public dbset<news> news { get; set; }      public groupnews()         : base("mydb")     {     } }    modelbuilder.entity<group>().      hasmany(c => c.relatednews).      withmany(p => p.relatedgroups).      map(       m =>       {           m.totable("groupnews");           m.mapleftkey("groupid");           m.maprightkey("newsid");       }); 

also in news entity defined :

  public virtual icollection<group> groups      {          get;          set;      } 

and same thing news. saving groupid , newsid groupnews table add 2 lines createevent method in eventcontroller:

        meetingboard.dal.meetingboardcontext.groupnews.eventid = happening.eventid;         group p = new group();         meetingboard.dal.meetingboardcontext.groupnews.groupid = p.groupid; 

and here createevent method :

public void createevent(string json_data) {     event happening = event.fromjson(json_data);      if (happening.eventid == 0)     {         happening.creatorid = userhelper.getcurrentuser().userid;          eventservice.createevent(happening);          projecttag_service.addprojecttag(happening);          broadcastcontext.clients.triggerchange("event");     }     else     {         event saved_happening = eventservice.getevent(happening.eventid);          saved_happening.title = happening.title;         saved_happening.description = happening.description;         saved_happening.location = happening.location;         saved_happening.startdate = happening.startdate;         saved_happening.enddate = happening.enddate;         eventservice.saveevent();          projecttag_service.removesprojecttagsbyeventid(happening.eventid);         projecttag_service.addprojecttag(happening);         meetingboard.dal.meetingboardcontext.groupevents.eventid = happening.eventid;         group p = new group();         meetingboard.dal.meetingboardcontext.groupevents.groupid = p.groupid;         broadcastcontext.clients.triggerchange("event_force");     } } 

but still groupnews table not updating , not see added news in page.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -