同一 edmx 中的 EF4 自定义实体
我有一个 edmx 文件,它 100% 反映我的数据库架构。 除此之外,我还需要根据现有实体创建一些自定义实体,非常类似于将多个实体的字段聚合为一个实体的视图。
问题是这不太有效。我尝试了几种方法,但它总是让我与 edmx 上已有的实际实体发生冲突。
我需要拥有反映我的数据库架构的那些实体,所以我是否必须创建另一个 edmx 文件来保存我的自定义实体并避免冲突?
我还想:
- 创建一个存储过程,但是如果 我需要过滤 SP 结果 I 伊瑟必须添加对搜索的支持 在获取所有行的 SP 上 使用 Linq2Objects 进行过滤...不行 这!
- 创建一个视图,这个视图将 工作得很好,但我想尝试一下 使用 EF4 来执行此操作 设计师并将所有内容合二为一 地方。
有人能给我举一些例子吗?
I have an edmx file that reflects 100% my DB schema.
Along with this I need to create some custom Entities based on the existent, pretty much like a view that will aggregate fields of several entites into a single one.
The problem is that this isn't quite working. I tried several approaches but it always gave me conflicts with the actual entites already on the edmx.
I need to have those entities that reflect my DB schema, so do I have to create another edmx file to hold my custom entites and avoid colisions?
I also though of:
- create a stored procedure but then if
I need to filter the SP result I
eather have to add support for serach
on the SP of get all the rows and
filter with Linq2Objects... won't do
this! - create a View, and this one would
work pretty well but I want to try
to do this making use of the EF4
designer and keep everything in one
place.
Could anyone point me to some examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你所描述的是一种观点,所以这可能是正确的方法。
您可以使用 DefiningQuery 节点将视图代码存储在 edmx 中。您不需要在数据库中创建视图。然而,设计者不支持此功能,您需要手动编辑 edmx。如果您使用设计器从数据库刷新 edmx,您所做的更改应该会保留。
详细信息请参见:
http://msdn.microsoft.com/en-us/library/ cc982038.aspx
http://blogs.msdn.com/b/davidebb/archive/2010/10/01/the-easy-way-to-create-an-entity-framework- defining-query.aspx
在数据库中创建视图,然后让设计者查找它们并对其建模会更容易。
该实体将是只读的,但当然,如果您想通过此视图支持修改,则可以为 UPDATE/INSERT/DELETE 分配 SP。
I think what you are describing is a view so this is probably the right way to go.
You can store the view code in the edmx using the DefiningQuery node. You don't need to create the view in the database. However there is no designer support for this feature, you will need to hand edit the edmx. The changes you make should be persisted if you refresh the edmx from the database using the designer.
Details here:
http://msdn.microsoft.com/en-us/library/cc982038.aspx
http://blogs.msdn.com/b/davidebb/archive/2010/10/01/the-easy-way-to-create-an-entity-framework-defining-query.aspx
It would be easier just to create the views in the database, and let the designer find and model them.
This entity will be read-only, but of course you can then assign SPs for UPDATE/INSERT/DELETE if you want to support modifications via this view.
除了特殊情况(表拆分、层次结构映射)之外,您不能基于同一个表定义两个实体。在这种情况下,您必须按照@James的建议使用DefiningQuery或QueryView。不同的是DefiningQuery是在存储模型中定义的,是普通的SQL。 QueryView 在概念模型中定义,并且是在现有实体之上定义的 ESQL。 QueryView 仅支持 ESQL 的某些功能(例如,它不支持聚合函数)。在这两种情况下,您都必须直接修改 EDMX (XML),设计器不支持这些功能。
如果您不想使用这些高级 EF 功能,@James 提到的数据库视图也是一个选项。您还可以简单地公开对象上下文上的预定义查询并将返回投影映射到自定义类型。
请注意,这些方法都不允许您修改、插入或删除数据。
You can't define two entities based on same table except special cases (table splitting, hiearchy mapping). In this case you have to use DefiningQuery as @James suggested or QueryView. The difference is that DefiningQuery is defined in storage model and it is common SQL. QueryView is defined in conceptual model and it is ESQL defined on top of already existing entities. QueryView supports only some features of ESQL (for example it doesn't support aggregate functions). In both cases you have to modify EDMX directly (XML), these features are not supported in designer.
Database view mentioned by @James is also an option if you don't want to use these advanced EF features. You can as well simply expose predefined queries on your object context and map return projection to custom type.
Be aware that neither of these methods will allow you to modify, insert or delete data.