Petapoco - 扩展 T4 模板生成的 poco 对象

发布于 2024-12-21 07:52:52 字数 1485 浏览 5 评论 0原文

对于一个新项目,我开始使用 PetaPoco T4 模板,其中 GenerateOperationsGeneratePocosGenerateCommon all = true。

但是,我想自定义生成的 poco 对象。如果我修改生成的类,每次修改架构时它都会被清除。扩展生成的 poco 对象的最佳方法是什么?


正如 Schotime 所说,我认为解决方案可能是使用部分类,但我很难让它发挥作用。 这是我的代码结构。

在 Database.cs 文件中(由 T4 模板自动生成)

namespace PetaPocoNISEntities
{
    public partial class PetaPocoNISEntities : Database
    {

        [TableName("GivenQuestion")]
        [PrimaryKey("Id")]
        [ExplicitColumns]    
        public partial class GivenQuestion : PetaPocoNISEntities.Record<GivenQuestion>  
        {
            [Column] 
            public int Id             
            { 
                ....
            }
        }
}

}

所以我创建了以下部分类。

namespace PetaPocoNISEntities
{
    public partial class PetaPocoNISEntities : Database
    {
        public partial class GivenQuestion : PetaPocoNISEntities.Record<GivenQuestion>

            [ResultColumn]
            public QuestionRepository QuestionRepository { get; set; }

            [ResultColumn]
            public List<GivenAnswer> GivenAnswers { get; set; }
    }
}

正如您所看到的,我使用了相同的名称空间。

但是当我尝试使用该类时,以下代码抱怨 gq (即 GiveQuestion)不包含 QuestinoRepository 方法?

var givenAnswers = results.Where(gq => gq.QuestionRepository .QuestionCode.Trim().ToUpper() == _formData.Trim().ToUpper());

有什么想法吗?

For a new project I started using PetaPoco T4 template with GenerateOperations, GeneratePocos, and GenerateCommon all = true.

However, I want to customize the poco object generated. If I modify the class generated it will be wiped out every time I modify the schema. What's the best way to extend the generated poco objects?


As Schotime said, I think the solution might be using partial class but I am having a hard time to get that working.
Here is my code structure.

in Database.cs file (autogenerated by T4 template)

namespace PetaPocoNISEntities
{
    public partial class PetaPocoNISEntities : Database
    {

        [TableName("GivenQuestion")]
        [PrimaryKey("Id")]
        [ExplicitColumns]    
        public partial class GivenQuestion : PetaPocoNISEntities.Record<GivenQuestion>  
        {
            [Column] 
            public int Id             
            { 
                ....
            }
        }
}

}

so I created the following partial classes.

namespace PetaPocoNISEntities
{
    public partial class PetaPocoNISEntities : Database
    {
        public partial class GivenQuestion : PetaPocoNISEntities.Record<GivenQuestion>

            [ResultColumn]
            public QuestionRepository QuestionRepository { get; set; }

            [ResultColumn]
            public List<GivenAnswer> GivenAnswers { get; set; }
    }
}

As you can see I used the same name space.

but then when I tried to use the class, the following code complains that the gq (which is GivenQuestion) doesn not contain the QuestinoRepository method?

var givenAnswers = results.Where(gq => gq.QuestionRepository .QuestionCode.Trim().ToUpper() == _formData.Trim().ToUpper());

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

城歌 2024-12-28 07:52:52

您可以使用 partial 类。
然后您可以修改分部类以添加额外的信息。

You could use partial classes.
Then you could modify the partial class to add extra information.

吻泪 2024-12-28 07:52:52

您的类不应该像您编写的那样嵌套。我在使用部分类扩展 POCO 对象时没有遇到任何问题。

另外,我会用 [PetaPoco.Ignore] 装饰添加的列,如下所示:

public partial class GivenQuestion
{
    [PetaPoco.Ignore]
    public QuestionRepository QuestionRepository { get; set; }
    [PetaPoco.Ignore]
    public List<GivenAnswer> GivenAnswers { get; set; }
}

Your classes should not be nested as you have them written. I've had no trouble extending my POCO objects with partial classes.

Also, I'd decorate the added columns with [PetaPoco.Ignore] as in:

public partial class GivenQuestion
{
    [PetaPoco.Ignore]
    public QuestionRepository QuestionRepository { get; set; }
    [PetaPoco.Ignore]
    public List<GivenAnswer> GivenAnswers { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文