gpt4 book ai didi

.net - 从 EF 模型中删除必需的属性,因为它是只读的

转载 作者:行者123 更新时间:2023-12-02 13:48:03 24 4
gpt4 key购买 nike

我有一个包含小型数据模型的项目,该项目以只读方式使用 EF 模型。

我不需要模型中的完整列集,但如果它们不可为空并且没有默认值,我就需要拥有它们。

如何避免包含此类列?我可以将 EF 置于数据模型中的某种只读模式,从而允许我从实体中删除列吗?

我想这样做的原因是,通过将数据模型中的列减少到我需要的数量,我可以减少模型需要在查询中返回的列,并且可以降低架构更改时破坏数据使用者的风险.

编辑:我的架构的表中的NOT NULL列没有默认值。据我所知,我需要将这些列包含在我的 edmx 中。在我的情况下,我只有只读上下文,因此我根本不希望这些列包含在我的 edmx 中。

如果我可以防止列出现在数据模型中,我就可以防止因更改架构而引起的许多问题。到目前为止,我找到的唯一解决方案是通过指向没有列的“假”数据库来构建数据模型!

最佳答案

根据 MSDN,QueryView 专为您描述的场景而设计。

The QueryView element in mapping specification language (MSL) defines a read-only mapping between an entity type or association in the conceptual model and a table in the underlying database.

You can define query views to enable the following scenarios:

Define an entity in the conceptual model that doesn't include all the properties of the entity in the storage model. This includes properties that do not have default values and do not support null values.

...(more scenarios on the page)

您无法使用设计器来完成此操作,但它看起来很简单,可以手动完成。

以下是相关 MSDN 文档的链接:
https://msdn.microsoft.com/en-us/library/cc716798(v=vs.100).aspx

如果该链接失效,请搜索 QueryView MSL

关于.net - 从 EF 模型中删除必需的属性,因为它是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com