【问题标题】:How can I update object that contains other objects in lists with npoco?如何使用 npoco 更新包含列表中其他对象的对象?
【发布时间】:2014-10-28 09:43:47
【问题描述】:

我有一个包含一些属性的类,其中包含一个包含其他类对象的列表。

[TableName("tblItem")]
[PrimaryKey("ITM_Id", AutoIncrement = false)]
[ExplicitColumns]
public class Item
{
    [Column("ITM_Id")]
    public Guid Id { get; set; }

    [Column("ITM_Name")]
    public string Name { get; set; }

    public List<PictureLink> PictureLink { get; set; }   
}

[TableName("tblPictureLink")]
[PrimaryKey("PIL_Id", AutoIncrement = false)]
[ExplicitColumns]
public class PictureLink
{
    [Column("PIL_Id")]
    public Guid Id { get; set; }

    [Column("PIL_InformationType")]
    public string InformationType { get; set; }
}

我想用 db.Update(Item) 之类的 npoco 进行更新 - 但是只有属性 Id 和 Name 得到更新,我怎样才能在同一个语句中使用 PictureLinks 更新列表?

【问题讨论】:

    标签: c# .net npoco


    【解决方案1】:

    将子对象的更新包含在一个循环中,如下所示:

     using (var scope = db.GetTransaction())
     {
         db.Update(item);
         foreach (var pictureLink in item.PictureLinks)
         {
             db.Update(pictureLink);
         }
         scope.Complete();
      }
    

    将更新包含在事务中将确保在出现任何问题时回滚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2020-11-20
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多