如何不先持久化属性 EF4 代码?
如何使用 codefirst EF4 创建非持久属性?
MS说有一个StoreIgnore属性,但我找不到它。
有吗一种使用 EntityConfiguration 进行设置的方法?
How do I make non persisted properties using codefirst EF4?
MS says there is a StoreIgnore Attribute, but I cannot find it.
Is there a way to set this up using EntityConfiguration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 EF Code-First CTP5 中,您可以使用
[NotMapped]
注释。In EF Code-First CTP5, you can use the
[NotMapped]
annotation.目前,我知道有两种方法可以做到这一点。
将“dynamic”关键字添加到属性中,这会阻止映射器保留它:
重写 DBContext 中的 OnModelCreating 并重新映射整个类型,忽略您不想保留的属性:< /p>
使用方法 2,如果 EF 团队引入了 Ignore,您将能够轻松地将代码更改为:
Currently, I know of two ways to do it.
Add the 'dynamic' keyword to the property, which stops the mapper persisting it:
Override OnModelCreating in DBContext and remap the whole type, omitting the properties you don't want to persist:
Using method 2, if the EF team introduce Ignore, you will be able to easily change the code to:
如果您不想使用注释,可以使用 流畅的API。重写
OnModelCreating
并使用 DbModelBuilder 的Ignore()
方法。假设您有一个“歌曲”实体:您还可以使用 EntityTypeConfiguration 将配置移动到单独的类以获得更好的可管理性:
If you don't want to use Annotations, you can use the Fluent API. Override the
OnModelCreating
and use DbModelBuilder'sIgnore()
method. Supposing you have a 'Song' entity:You can also use EntityTypeConfiguration to move configurations to separate classes for better manageability:
我不确定这是否可用。
在此 MSDN 页面 描述了忽略属性和 API,但在下面的评论中,有人于 2010 年 6 月 4 日写道:
I'm not sure if this is available yet.
On this MSDN page the Ignore Attribute and API are described but below, in the comments, somebody writes on 4 june 2010:
添加
使用 System.ComponentModel.DataAnnotations.Schema
到模型类。 (必须包含“SCHEMA”)
将 [NotMapped] 数据注释添加到您想要保留的字段(即不保存到数据库)。
这将阻止它们作为列添加到数据库中的表中。
请注意 - 以前的答案可能包含这些位,但它们没有完整的“使用”子句。他们只是放弃了“模式”——在该模式下定义了 NotMapped 属性。
Add
using System.ComponentModel.DataAnnotations.Schema
to the model class. (Must include "SCHEMA")
Add [NotMapped] data annotation to the field(s) you want to keep from persisting (ie. not save to database).
This will prevent them from being added as a column to the table in the db.
Please note - previous answers may have included these bits, but they did not have the full "using" clause. They merely left off "schema" - under which the NotMapped attribute is defined.