简体   繁体   English

Linq不在中间多对多关系表中插入记录

[英]Linq not inserting record for the middle many-to-many relationship table

I am having creating a new record that will be inserted into the DB. 我正在创建一个将插入数据库的新记录。 I looked at the db.log but nothing gets printed. 我看了看db.log,但什么都没打印。 I have primary keys marked, but not sure what else needs to be done. 我已经标记了主键,但是不确定还需要做什么。

Have a many-to-many relationship between two tables (Member and RecForms). 在两个表(Member和RecForms)之间具有多对多关系。 This is being down through a middle table of MemberRecForms that contains the ID for the other tables. 这是通过MemberRecForms中间表进行的,该中间表包含其他表的ID。 In the MemberRecForm table the two keys are marked as compound primiary keys (it is also this way in the dbml). 在MemberRecForm表中,这两个键被标记为复合主键(在dbml中也是如此)。

          DataContext db = new DataContext();

          MemberRecForm r = new MemberRecForm();

          r.RecFormID = 2;

          this.MemberRecForms.Add(r);

          try
          {
            db.SubmitChanges(ConflictMode.ContinueOnConflict);
          }
          catch (ChangeConflictException)
          {
            db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
          }

When I look at r after I do this.MemberRecForms.Add(r) I see that r was updated with the correct memberID. 当我这样做之后查看r时,MemberRecForms.Add(r)我看到r已使用正确的memberID更新。

Change "this.MemberRecForms.Add(r);" 更改“ this.MemberRecForms.Add(r);” to "db.MemberRecForms.InsertOnSubmit(r);". 到“ db.MemberRecForms.InsertOnSubmit(r);”。 Otherwise the datacontext will not have a reference to it, and will not insert it... 否则,数据上下文将没有对其的引用,也不会插入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM