了解 POCO 生成器
我试图了解 .NET 中的 poco 生成器应该做什么。在我看来,它们应该是代码生成工具,允许我从 edmx 文件中选择我想要为其创建 POCO 的实体,并根据表的字段生成类,同时允许我自定义生成的 poco 名称,因此它是不同的来自我的 edmx Designer.cs 文件中生成的类名。例如:我有一个名为 Customer 的表,它在 edmx 文件中被命名为 Customers。现在 poco 生成工具应该允许我创建一个名为 BusinessCustomers 的类。
然而,虽然他们在不允许我自定义生成的类名称的情况下生成了一些类,但我注意到,当从 这个示例,它们往往是仍然由代码生成工具拥有的类。生成的 poco 类是一个名为 Customers 的分部类,它基本上是 edmx 类的扩展。我无法选择性地选择要为其生成 pocos 的实体。因此,如果我对生成的类进行了更改,那么每当我将新表添加到 edmx 文件并想要使用该工具创建新的 pocos 时,它们都会丢失。我当然可以将生成的类复制到另一个项目并从那里使用它们,但我不确定这是否是正确的方法。 在上面的示例中,生成的 poco 类是一个名为 Customers 的分部类,具有虚拟属性,表中的每个字段都有一个虚拟属性。 edmx Designer.cs 已被清除。我仍将在代码中使用该工具生成的类。如果我想要一个单独的 poco 类,我仍然需要自己编写它...... poco 生成器不是应该为我创建一组新的类,而使 edmx 类保持不变吗?
至少,我想要的只是为我生成类的工具,并且在生成完成后不再触及它。如果我正确理解了 ADO.NET C# POCO 实体生成器,它就不会做那样的工作。是否有更好的工具,或者我应该坚持根据我的要求手动编写 poco 类?你怎么认为?
抱歉,如果我的问题有点令人困惑;我在写这篇文章时对这个主题的理解有限,甚至可能是不正确的。感谢您抽出时间...
I am trying to get an understanding of what poco generators in .NET are supposed to do. In my opinion, they should be code generation tools that will allow me to select entities from my edmx file that I want to create a POCO for and generate classes based on the table's fields while allowing me to customize the generated poco name so it is different from the class name generated in my edmx designer.cs file. Say for eg: I have a table called Customer which in the edmx file gets named as Customers. Now the poco generation tool should allow me to create a class called say, BusinessCustomers.
However, while they do that bit of generating classes without allowing me to customize the generated class name, I have noticed that when generating such pocos from this example, they tend to be classes that are still owned by the code generation tool. The generated poco class is a partial class named as Customers, which is basically an Extension of the edmx class. I cannot selectively choose what entities I want to generate pocos for. So in case I have made changes to the generated class they will be lost anytime I add new tables to the edmx file and want to create new pocos using the tool. I can of course copy the generated classes to another project and use them from there, but I am not sure if that is the right way.
In my example above, the generated poco class is a partial class named as Customers, with virtual properties, one for each field in the table. The edmx designer.cs is wiped clean. I am still going to be using a class generated by the tool in my code. If I want a separate poco class I will still have to write it myself... Wasn't the poco generator supposed to create a new set of classes for me leaving the edmx classes intact?
At the bare minimum all I want is the tool to generate classes for me and not touch it again once it is done with generating it. If I have understood the ADO.NET C# POCO Entity Generator correctly, it doesn't do the job like that. Are there better tools out there or should I just stick to hand coding my poco classes for my requirements? What do you think?
Sorry if my question is a bit confusing; I am writing this with limited and perhaps incorrect understanding of the subject . Thanks for your time ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
POCO 类生成器可帮助您通过更改生成它们的 T4 模板来自定义生成过程,并将它们与包含 EDMX 的项目分开。
这对于测试来说非常有帮助,您可以用自定义生成器或其他一些数据馈送结构替换 EDMX,这些结构填充生成的类,这些类可能位于与 edmx 本身完全不同的项目中。
至于定制,您所要做的就是将这些类声明为 Partial(不确定这是否是默认的,但可以在 T4 模板中轻松更改)。因此,您要做的就是使用另一个文件来表示包含自定义代码的类的部分。这样您就可以自定义您的类,而在重新生成它们时不会丢失更改。
答案如此长,您应该坚持使用 POCO Generator,因为它简化了将数据库表映射到类的整个过程。
POCO class generators are there to help you customize the generation process by altering the T4 template that generates them and also break them apart from the EDMX containing project.
This can be really helpful for say testing, where you replace the EDMX with a custom generator or some other data feeding structure that populates your generated classes which can be in a wholly different project than the edmx itself.
As for customization all you have to do is declare those classes as Partial (not sure if that's the default, but can easily alter that in the T4 template). So what you do is have another file representing the part of the class holding the custom code. That way you can customize your classes without loosing changes when re-generating them.
So long answer short, you should stick with POCO Generator, as it simplifies the whole process of mapping DB tables to classes.