如何与Dapper插入相关对象的外键ID?

发布于 2025-02-04 17:47:31 字数 1644 浏览 3 评论 0原文

我有以下类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-02-11 17:47:31

通常,在这些情况下,我要做的是创建两组模型。 “ DataAccess”模型,最终用户的数据库和“域”模型为1-1。此外,创建从域到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);
    }
}


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.

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);
    }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文