如何在实体框架中禁用属性的自动映射 [英] How to disable automapping of properties in Entity Framework

查看:62
本文介绍了如何在实体框架中禁用属性的自动映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在Entity Framework中使用流畅的映射。我的意图是通过代码映射所有内容,而没有任何属性和自动映射功能。我发现最好的方法是为项目中的每个实体实现EntityTypeConfiguration类。

I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project.

稍后,我将财产添加到我的一个实体中。不需要保留此属性。我期望,在为该属性添加映射之前,数据库和持久层将忽略它。不幸的是,这种方式不起作用,并且属性已映射。唯一方法是使用忽略方法或NotMapped属性,但我不想明确地做到这一点。

Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and property is mapped. Only way is to use Ignore method or NotMapped attribute, but I don't want to do it explicitly.

有什么方法可以阻止Entity Framework自动映射吗?我试图从DbModelBuilder中删除所有约定,但这无济于事。

Is there any way, to stop Entity Framework from automapping? I've tried to remove all Conventions from DbModelBuilder, but it doesn't help.

推荐答案

据我所知,别无选择。您需要使用 Ignore() [NotMapped] 。我倾向于使用前者,因为它不会使模型混乱。

So far as I am aware, there is no other way around it. You need to use either Ignore() or [NotMapped]. I tend to prefer the former as it does not clutter up the model.

这篇关于如何在实体框架中禁用属性的自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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