为特定应用程序建模实体 - 实体框架

[英]Modeling entities for specific applications - Entity Framework


I have 2 applications:

我有2个申请:

  1. Manager UI - Which should be able to create a user with all its fields (username, email, age etc.) and display them.
  2. 管理器UI - 应该能够创建一个包含其所有字段(用户名,电子邮件,年龄等)的用户并显示它们。

  3. Core component - which only need the UserId and Username field, and need to be very performant. It also shouldn't be able to write to the database.
  4. 核心组件 - 只需要UserId和Username字段,并且需要非常高效。它也应该无法写入数据库。

It seems I need 2 different models, one for the UI application and a second one for the core component. When I create the 1st model, life's good. But when I try to create the 2nd model, I get all the unneccessary fields. If I try to delete them - I manage to do that only for the nullable fields, but not for the required ones (which is only required for saving, actually). Here's what I get: "Column Users.email in table Users must be mapped: It has no default value and is not nullable."

我似乎需要2个不同的模型,一个用于UI应用程序,另一个用于核心组件。当我创造第一个模型时,生活是美好的。但是当我尝试创建第二个模型时,我得到了所有不必要的字段。如果我尝试删除它们 - 我设法只对可空字段执行此操作,而不是为必需的字段执行(实际上只需要保存)。这就是我得到的:“表中的Users Users.email必须映射用户:它没有默认值且不可为空。”

Setting the "email" field as private getter & setter will not solve my issue, since I don't want to store the value at all on the entity (and fetch the field from the database) - as I said, this application needs to be as performant as possible.

将“email”字段设置为私有getter&setter将无法解决我的问题,因为我不想将值存储在实体上(并从数据库中获取字段) - 正如我所说,此应用程序需要尽可能高效。

I believe that if I could somehow set the model as read-only, it will solve this, but it's only a hunch (and I couldn't find a way to do that - is therer?)

我相信,如果我能以某种方式将模型设置为只读,它将解决这个问题,但它只是一种预感(我找不到办法做到这一点 - 是吗?)

Any help would be appreciated. Thanks, Nir.

任何帮助,将不胜感激。谢谢,Nir。

2 个解决方案

#1


There is no such thing as a "read-only" entity model. You can, however, do a read-only query by setting the MergeOption value so that changes to entities are not tracked. You can also select only the columns you need in your application by projecting into an anonymous type instead of returning an entity type in your queries. So you would use the same model in both applications, but in the "core component" you would use the model differently, in a way which delivers better performance for read-only use.

没有“只读”实体模型这样的东西。但是,您可以通过设置MergeOption值来执行只读查询,以便不跟踪对实体的更改。您还可以通过投影到匿名类型而不是在查询中返回实体类型来仅选择应用程序中所需的列。因此,您将在两个应用程序中使用相同的模型,但在“核心组件”中,您将以不同的方式使用模型,从而为只读使用提供更好的性能。

#2


EF was really designed for you to gen the model for each specific use case. You are not supposed to need to gen a single large monolithic model because they are so easy to build and update (that was the theory anyway).

EF真的是为你设计每个特定用例的模型而设计的。您不应该需要生成单个大型单片模型,因为它们很容易构建和更新(无论如何这是理论)。

So I would say you should generate 2 models - one for each use case scenario. This means you only need to include what is absolutely necessary for each use case.

所以我会说你应该生成2个模型 - 每个用例场景一个。这意味着您只需要包含每个用例绝对必要的内容。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/07/10/39f6f4188023f3e25cc6f737e898aecb.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告