我有一个新闻实体,我定义了一个约束,以使用 GroupNews 表根据他们的 GroupID 获取新闻。现在,当我添加新新闻时,因为 GroupNews 表中没有添加任何内容,新闻不会显示在网页中。我需要将 NewsID 和 GroupID 添加到 GroupNews 表中。
我在 mywebContect.cs 中定义了 GroupNews :
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");
});
同样在我定义的新闻实体中:
public virtual ICollection<Group> Groups
{
get;
set;
}
新闻也是如此。为了将 GroupID 和 NewsID 保存到 GroupNews 表中,我在 EventController 中的 CreateEvent 方法中添加了两行:
MeetingBoard.DAL.MeetingBoardContext.GroupNews.EventID = happening.EventID;
Group p = new Group();
MeetingBoard.DAL.MeetingBoardContext.GroupNews.GroupID = p.GroupID;
这是 CreateEvent 方法:
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");
}
}
但是 GroupNews 表仍然没有更新,我在页面中看不到添加的新闻。