【问题标题】:How to update nested objects in NPoco如何更新 NPoco 中的嵌套对象
【发布时间】:2016-08-26 17:31:20
【问题描述】:

我找不到在多对一情况下更新嵌套对象的示例。映射在 Fetch 上的工作方式非常清楚;而且我不是在说更新嵌套对象的集合,只是一个。所以给出下面的例子:

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    [ResultColumn]
    public School CurrentSchool { get; set; }
}

public class School
{
    public int SchoolId { get; set; }
    public string Name { get; set; }
}

在数据库中,Student 表带有 CurrentSchool 列,该列是 School 表中记录的 ID。

我没有更新 School 对象本身,而是将学生映射到另一个对象。这在 Query 上很好,但是如果我更改 Student 上的 CurrentSchool 对象并使用 db.Update(studentObject) 保存,则外键不会更新。

【问题讨论】:

    标签: c# petapoco micro-orm npoco


    【解决方案1】:

    单步执行源码后,我发现 NPoco.ColumnInfo 类上有一个 ReferenceType 属性。搜索这个,你瞧,NPoco wiki 中有文档位于 Version 3 -> Relationships (https://github.com/schotime/NPoco/wiki/Version-3#relationships) 下。

    如果我添加:

    [NPoco.Reference(NPoco.ReferenceType.Foreign, ColumnName = "SchoolId", ReferenceMemberName = "SchoolId")]
    public School CurrentSchool { get; set; }
    

    那么当Student对象被持久化在数据库中时,外键关系就被保存了。

    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 1970-01-01
      • 2019-10-13
      • 1970-01-01
      • 2022-12-21
      • 2021-01-23
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多