如何与Dapper插入相关对象的外键ID?
我有以下类:
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
表。非常标准的东西。
我正在使用Dappper尝试插入一个填充的模板
so ...
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多个属性。
任何帮助都非常感谢。
I have the following classes:
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; }
}
My database tables are predominantly the same except my Template
table has a foreign key of TemplateTypeId
which stores the Id
from the TemplateType
table. Pretty standard stuff.
I am using Dappper to try to insert a fully populated Template
object like so...
string sql = $@"insert into dbo.Template (Title, SignaturePerLine, StandardTime, StartDate, SpecialTest)
values (@Title, @SignaturePerLine, @StandardTime, @StartDate, @SpecialTest);";
connection.Execute(sql,Template);
The issue...
My problem occurs when I try to add in the TemplateTypeId
.
How can I tell Dapper to insert this?
I had hoped I could do something like this:
string sql = $@"insert into dbo.Template (Title, SignaturePerLine, StandardTime, StartDate, SpecialTest, TemplateTypeId)
values (@Title, @SignaturePerLine, @StandardTime, @StartDate, @SpecialTest, @TemplateType.Id);";
But that does not work.
I could maybe add each property as a set of DynamicParameters
but I was hoping to avoid that approach as some of my other classes have 20+ properties.
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,在这些情况下,我要做的是创建两组模型。 “ DataAccess”模型,最终用户的数据库和“域”模型为1-1。此外,创建从域到dataAccess,反之亦然的映射。
正如您提到的,拥有具有更多20个属性的模型,这可以帮助您更简单地管理数据。
Usually what I do in these situations is create two sets of models. 'DataAccess' models, which are 1 to 1 with the database and 'Domain' models for end user. Furthermore create mappings from domain to dataAccess and vice-versa.
As you mentioned, having models with more 20 properties, this helps you manage the data more easly.