我正在尝试运行嵌套映射,但嵌套对象为空。这意味着在我的情况下result[0].Location
是空的。我将 NPoco 3.3.0-beta3 与 Postgres 一起使用。
这里的代码片段:
var db = new Database(new NpgsqlConnection(configurations["ConnectionStrings:Database"]));
var sql = Sql.Builder
.Append("SELECT r.*, l.* FROM race r")
.Append("INNER JOIN location l ON r.location_id = l.id");
using (db.Connection)
{
db.Connection.Open();
var result = Db.Fetch<RaceEntity>(sql);
// result[0].Location == null
}
种族实体:
[TableName("race")]
[PrimaryKey("id", AutoIncrement = true)]
public class RaceEntity
{
[Column("id")]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; }
[Column("location_id")]
public int LocationId { get; set; }
[Column("date")]
public DateTime Date { get; set; }
[ResultColumn]
public LocationEntity Location { get; set; }
}
位置实体
[TableName("location")]
[PrimaryKey("id", AutoIncrement = true)]
public class LocationEntity
{
[Column("id")]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; }
[Column("province")]
public string Province { get; set; }
[Column("postal")]
public string Postal { get; set; }
[Column("country")]
public string Country { get; set; }
[Column("iso_code")]
public string IsoCode { get; set; }
}
比赛表
CREATE TABLE "race" (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(100) NOT NULL,
"location_id" INTEGER REFERENCES "location" (id),
"date" date NOT NULL
);
位置表
CREATE TABLE "location" (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(100) NOT NULL,
"province" VARCHAR(100) NULL DEFAULT NULL,
"postal" VARCHAR(30) NULL DEFAULT NULL,
"country" VARCHAR(70) NOT NULL,
"iso_code" VARCHAR(10) NULL DEFAULT NULL
);