首页 新闻 会员 周边

郁闷了关于EF中更新多个表的问题,请各位指教

0
悬赏园豆:50 [待解决问题]

我有一个需求,更新的时候,可能需要同时更新父表与子表的数据,1对多的关系。

假定以主题与回复为例

public class BBSTOPIC
 {
     public BBSTOPIC()
  {
   this.BBSReplies = new List<BBSReply>();
  }

  public int TID { get; set; }
  public int UID { get; set; }
  public int ReplyCount { get; set; }
  public string topic { get; set; }
  public string contents { get; set; }
  public System.DateTime times { get; set; }
  public int clickcount { get; set; }
  public virtual ICollection<BBSReply> BBSReplies { get; set; }
  public virtual person person { get; set; }
 }

回复:

 public class BBSReply
 {
  public int RID { get; set; }
  public int TID { get; set; }
  public int UID { get; set; }
  public string Rtopic { get; set; }
  public string Rcontents { get; set; }
  public System.DateTime Rtime { get; set; }
  public int Rclickcount { get; set; }
  public string Uname { get; set; }
  public virtual BBSTOPIC BBSTOPIC { get; set; }
  public virtual person person { get; set; }
 }

 

dao:

public virtual void Update(T entity)
    {       

       dataContext.Set<T>().Attach(entity);       
        dataContext.Entry(entity).State = EntityState.Modified;
    }

service:

 public void EditBbstopics(BBSTOPIC bbstopics)
       {
           BBSReply bbsr = new BBSReply();
           bbsr.TID = bbstopics.TID;  //主题主键
           bbsr.RID = 1;                   //回复主键
           bbsr.Rcontents = "aaaaaaaaaaa";
           bbsr.Rclickcount = 10;
           bbsr.Rtime = DateTime.Parse("2012-2-1");
           bbsr.Rtopic = "bbbbbbb";
           bbsr.UID=2;                        //用户ID
           bbsr.Uname = "test";
           bbsr.BBSTOPIC = bbstopics;   //导航指向主题

           bbstopics.BBSReplies.Add(bbsr);   //将了表加到主表中去

          bbstopicsrespository.Update(bbstopics);   //调用update方法上面的

          bbstopicsrespository.savechange();    //提交至数据库,提交后我查数据库只有主表就是BBSTOPIC更新成功了,BBSReply却没更新。

        同样的方法我改成add可以新增,但是要改为update,却只能更新bbstopics
,却更新不了bbsr回复表,为什么呢?我指定了回复表的主键ID还有主题ID,还有主题,它却不能更新呢?希望各位指教一下。谢谢          

       }

ef
农村的芬芳的主页 农村的芬芳 | 初学一级 | 园豆:160
提问于:2012-02-08 19:43
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册