没有某些字段的 POCO 实体(实体的自由建模)
我知道,在使用 POCO 实体时,您应该针对您的模型(POCO 实体)进行工作。另外,我认为针对此类模型进行编程的部分好处应该提供诸如定义与您在数据库中看到的内容不完全匹配的类之类的好处。
然而,有一些简单的操作我不知道如何做,但我认为它们应该是可能的。例如,在某些情况下,更改一列的名称(实体中的属性)可能很有用。另外我想知道是否可以生成仅表示支持数据库中对象的表的某些字段的 POCO 模型。
有没有关于此类操作的文档?
多谢!
I understand that, when working with POCO entities, you should work against your model (POCO Entities). Also I supose that part of the benefits of programming against models like those should provide benefits like defining classes that don't match exactly what you see in the db.
However, there are simple operations that I don't know how to do and that I assume they should be possible. For example, in some scenarios it can be useful to change the name of one column (atribute in the entity). Also I would like to know if it's possible to generate POCO models that only represents some fields of the table that supports the object in the db.
Is there any documentation about this kind of operations?
¡Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
POCO实体只是映射类。你问题中的模型意味着映射。映射的重点是定义类和数据库表之间的映射,包括属性和列之间的映射。因此,只要在映射中正确配置,您就可以拥有不同的属性和列名称。
因此,如果您使用 EDMX 文件(设计器)来生成映射,您只需更改属性或实体的名称,它将反映在生成的 POCO 实体中。 EDMX 文件也将正确更新映射。如果您首先使用代码,则必须通过数据注释或流畅的 API 手动定义映射。
实体应该代表持久化到数据库的单一数据结构。因此,每个表只能映射一次。此外,EDMX 设计者要求每个没有默认值的不可空列必须映射到实体。 POCO 生成器不是用于生成不同数据视图的工具。您正在寻找的称为投影。有多种方法可以在 EDMX 文件中包含映射投影(
DefiningQuery
和QueryView
),但这两种方法都需要手动修改 EDMX 文件,第一种方法还需要手动维护 EDMX 文件。如果您需要从实体中删除某些属性只是为了改进某些查询,或者因为某些操作不需要所有数据,您始终可以直接在查询中使用到匿名或自定义类的投影。
POCO 生成器只是为映射实体和投影生成类的工具,而不是生成您需要的所有数据相关类的工具。
POCO entity is just mapped class. The model in your question means mapping. The point of mapping is to define map between class and database table including mapping between properties and columns. So you can have different property and column names as long as it is correctly configured in mapping.
So if you are using EDMX file (designer) for generating the mapping you can simply change the name of property or entity and it will be reflected in your generated POCO entity. Also EDMX file will correctly update mapping. If you are using code first you must manually define mapping either through data annotations or through fluent API.
Entity should represent single data structure persisted to the database. Because of this each table can be mapped only once. Moreover EDMX designer demands that each non-nullable column without default value must be mapped to the entity. POCO generator is not tool for generating your different data views. What you are looking for is called projection. There are ways how to include mapped projections in EDMX file (
DefiningQuery
andQueryView
) but both requires manual modifications of EDMX file and the first one also requires manual maintenance of EDMX file.If you need to remove some properties from entity just to improve some query or because you don't need all data for some operation you can always use projection to anonymous or custom class directly in the query.
POCO generator is only tool for generating classes for mapped entities and projections not for generating all data related classes you will ever need.