[英]How can I insert foreign key id of a related object with Dapper?
我有以下課程:
public class Template
{
public int Id { get; set; }
public string Title { get; set; }
public bool SignaturePerLine { get; set; }
public string SpecialTest { get; set; }
public TemplateType TemplateType { get; set; } = new TemplateType();
public int StandardTime { get; set; }
public DateTime StartDate { get; set; }
}
public class TemplateType
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Pictures { get; set; }
public bool Video { get; set; }
public bool Matrix { get; set; }
}
我的數據庫表基本上是相同的,除了我的Template
表有一個TemplateTypeId
外鍵,它存儲TemplateType
表中的Id
。 很標准的東西。
我正在使用 Dappper 嘗試像這樣插入一個完全填充的Template
對象......
string sql = $@"insert into dbo.Template (Title, SignaturePerLine, StandardTime, StartDate, SpecialTest)
values (@Title, @SignaturePerLine, @StandardTime, @StartDate, @SpecialTest);";
connection.Execute(sql,Template);
問題...
當我嘗試添加TemplateTypeId
時,會出現我的問題。 我怎樣才能告訴 Dapper 插入這個?
我曾希望我能做這樣的事情:
string sql = $@"insert into dbo.Template (Title, SignaturePerLine, StandardTime, StartDate, SpecialTest, TemplateTypeId)
values (@Title, @SignaturePerLine, @StandardTime, @StartDate, @SpecialTest, @TemplateType.Id);";
但這不起作用。
我可以將每個屬性添加為一組DynamicParameters
但我希望避免這種方法,因為我的其他一些類有 20 多個屬性。
非常感謝任何幫助。
通常我在這些情況下所做的是創建兩組模型。 “DataAccess”模型,與數據庫和“域”模型為最終用戶一對一。 此外,創建從域到 dataAccess 的映射,反之亦然。
正如您所提到的,擁有具有更多 20 個屬性的模型,這可以幫助您更輕松地管理數據。
namespace DataAccess.Models;
public class TemplateDb
{
public TemplateDb(int id, string title, bool signaturePerLine, string specialTest, int templateTypeId, int standardTime, DateTime startDate)
{
Id = id;
Title = title ?? throw new ArgumentNullException(nameof(title));
SignaturePerLine = signaturePerLine;
SpecialTest = specialTest ?? throw new ArgumentNullException(nameof(specialTest));
TemplateTypeId = templateTypeId;
StandardTime = standardTime;
StartDate = startDate;
}
public int Id { get; set; }
public string Title { get; set; }
public bool SignaturePerLine { get; set; }
public string SpecialTest { get; set; }
public int TemplateTypeId { get; set; }
public int StandardTime { get; set; }
public DateTime StartDate { get; set; }
}
public class TemplateTypeDb
{
public TemplateTypeDb(int id, string name, string description, bool pictures, bool video, bool matrix)
{
Id = id;
Name = name ?? throw new ArgumentNullException(nameof(name));
Description = description ?? throw new ArgumentNullException(nameof(description));
Pictures = pictures;
Video = video;
Matrix = matrix;
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Pictures { get; set; }
public bool Video { get; set; }
public bool Matrix { get; set; }
}
namespace Domain.Models;
public class Template
{
public Template(int id, string title, bool signaturePerLine, string specialTest, TemplateType templateType, int standardTime, DateTime startDate)
{
Id = id;
Title = title ?? throw new ArgumentNullException(nameof(title));
SignaturePerLine = signaturePerLine;
SpecialTest = specialTest ?? throw new ArgumentNullException(nameof(specialTest));
TemplateType = templateType ?? throw new ArgumentNullException(nameof(templateType));
StandardTime = standardTime;
StartDate = startDate;
}
public int Id { get; set; }
public string Title { get; set; }
public bool SignaturePerLine { get; set; }
public string SpecialTest { get; set; }
public TemplateType TemplateType { get; set; }
public int StandardTime { get; set; }
public DateTime StartDate { get; set; }
}
public class TemplateType
{
public TemplateType(int id, string name, string description, bool pictures, bool video, bool matrix)
{
Id = id;
Name = name;
Description = description;
Pictures = pictures;
Video = video;
Matrix = matrix;
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Pictures { get; set; }
public bool Video { get; set; }
public bool Matrix { get; set; }
}
public class Mapper
{
public TemplateDb ToDb(Template template)
{
return new TemplateDb(
id: template.Id,
title: template.Title,
signaturePerLine: template.SignaturePerLine,
specialTest: template.SpecialTest,
templateTypeId: template.TemplateType.Id,
standardTime: template.StandardTime,
startDate: template.StartDate
);
}
public Template ToDomain(TemplateDb templateDb, TemplateTypeDb templateTypeDb)
{
return new Template(
id: templateDb.Id,
title: templateDb.Title,
signaturePerLine: templateDb.SignaturePerLine,
specialTest: templateDb.SpecialTest,
templateType: new TemplateType(
id: templateTypeDb.Id,
name: templateTypeDb.Name,
description: templateTypeDb.Description,
pictures: templateTypeDb.Pictures,
video: templateTypeDb.Video,
matrix: templateTypeDb.Matrix
),
standardTime: templateDb.StandardTime,
startDate: templateDb.StartDate);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.