Petapoco - 扩展 T4 模板生成的 poco 对象
对于一个新项目,我开始使用 PetaPoco T4 模板,其中 GenerateOperations
、GeneratePocos
和 GenerateCommon
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
partial
类。然后您可以修改分部类以添加额外的信息。
You could use
partial
classes.Then you could modify the partial class to add extra information.
您的类不应该像您编写的那样嵌套。我在使用部分类扩展 POCO 对象时没有遇到任何问题。
另外,我会用 [PetaPoco.Ignore] 装饰添加的列,如下所示:
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: