创建实体的视图 [英] Creating a view of an entity

查看:74
本文介绍了创建实体的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在实体框架中创建实体的视图,而不在DAL中创建视图?

Is it possible to create a view of an entity in entity framework without creating a view in DAL?

我有一个名为收据的父表收据可以是活动或非活动的。如果我实施 IsActive 作为收据的属性,那么很有可能忘记附加 .Where(r => r.IsActive) 到所有Linq查询,并将其添加到以前的代码的高成本。我尝试在模型中继承一个 DeletedReceipt 的条件 IsActive = false ,然后将IsActive = true 添加到Receipt(Parent)。幸福的生意并没有变化。问题是我无法写入Deactivate方法,而Receipt有很多重要的关系。我知道它不是面向对象的。我想我可以用它来处理它。但我不会改变我的DAL的商业方法!
的情况是 IsActive 播放Discriminator角色,并且在停用方法中不可更新。一种方法可能是使用SP,但这意味着DAL请处理我的死亡业务逻辑。
任何想法?

i have a parent table named Receipt. Receipt can be active or inactive. if i implement IsActive as an attribute of the Receipt, there's a high risk of forgetting to attach .Where(r=>r.IsActive) to all Linq queries and high cost of adding it to previous codes. i tried to inherit a child DeletedReceipt with condition IsActive=false in model and add condition IsActive=true to Receipt (Parent). Happily business didn't take any change. the problem is i can not write Deactivate method while Receipt have many important relations. and i know it's not object oriented. i think i can handle it with a view. but i don't change my DAL for a business method! the case is "IsActive" plays Discriminator role, and is not updatable in Deactivate method. one way may be using SP but that means "DAL please handle my damn Business Logic". any idea?

推荐答案

有两种方法来创建视图:

There are two ways to create view:


  • 定义查询 - 自定义SQL选择在SSDL部分的EDMX文件。您可以使用任何SQL来定义新的只读实体。问题是,一旦你使用这个,你不能再使用数据库中的更新,因为它将覆盖EDMX中的更改(除非你为EF设计师购买更强大的扩展)。

  • QueryView - 定制ESQL在EDMX文件的MSL部分中必须从现有实体中进行选择。它支持关系,但仅支持其他查询视图。

  • DefiningQuery - custom SQL select in SSDL part of EDMX file. You can use any SQL to define new read-only entity. The problem is that once you use this you cannot use update from database any more because it would overwrite your changes in EDMX (unless you buy some more powerful extension for EF designer).
  • QueryView - custom ESQL select in MSL part of EDMX file which must select from existing entities. It supports relations but only to other query views.

在这两种情况下,创建的实体都是一个view =只读。您不能使用它来保存更改,除非将更新,插入和删除操作映射到存储过程或自定义SQL(再次写入EDMX文件)。

In both cases created entity is a view = read-only. You cannot use it for saving changes unless you map update, insert and delete operations to stored procedure or custom SQL (again written in EDMX file).

无论如何,您正在尝试做会导致很多问题。您是否需要在应用程序中访问 DeletedReceipt ?如果是,您需要具有与 ActiveReceipt 完全相同的逻辑?如果不这样做的话。

Anyway what you are trying to do will lead to a lot of problems. Do you ever need to access DeletedReceipt in your application? If yes do you need to have full logic as you have with ActiveReceipt? If no don't do this.

问题是软删除的 IsActive 条件是EF不能很好地处理。您可以解决您的主要查询问题,您不需要使用 Where(r => r.IsActive),但是您是否曾经使用过加载或延迟加载加载任何其他实体的相关收据?如果是,您是否希望只加载活动收据?这是真正的问题。加载支持过滤既不急切也不懒惰,所以您将始终加载活动和删除的记录。处理此问题的唯一方法(手动完成所有操作)是条件映射,但条件映射允许您在应用程序中只有这些实体之一,并且您需要自定义存储过程进行软删除。

The problem is that IsActive condition for soft delete is something EF doesn't handle very well. You can solve the issue for your main query where you don't need to use Where(r => r.IsActive) but do you ever use eager loading or lazy loading to load related receipts for any other entities? If yes do you expect to load only active receipts? Here comes the real problem. Neither eager or lazy loading support filtering so you will always load both active and deleted records. The only way to handle this (without doing everything manually) is conditional mapping but conditional mapping allows you to have only one of these entities in your application and you need custom stored procedure for soft deleting.


DAL请处理我的业务逻辑

"DAL please handle my damn Business Logic"

作为该报价的附注,如果您不希望DAL处理任何逻辑,不要指望它只会自动为您提供活动收据。这也是DAL的逻辑。所以如果你真的意味着在所有查询中使用 Where(r => r.IsActive)是你正在寻找的。如果你想从DAL获得一些帮助,它必须包含一些逻辑,有时逻辑将在SQL中。

As a side note to this quote if you don't want DAL to handle any logic don't expect it to give you only active receipts automatically. That is a logic in DAL as well. So if you really mean that using Where(r => r.IsActive) in all queries is what you are looking for. If you want to have some help from DAL it must contain some logic and sometimes the logic would be in SQL.

这篇关于创建实体的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆