实体框架:忽略列 [英] Entity Framework: Ignore Columns

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

问题描述

我有一个我希望构建一个EF模型的数据库,但是我不想从数据库中包含某些列,因为相关列仅在服务器上进行维护,不应由任何应用程序进行操作。 p>

这两个列都是DateTime(如果这有任何区别),其中一列是可空的,并且由更新的触发器维护,另一个不可为空,并且使用表定义中的默认值。



我想我正在寻找类似Linq2Sql中的服务器生成选项;但是我找不到这样的选择。



有人可以告诉我如何解决这个问题吗?



警告:



我一直在努力在我的工作地点引入业务对象建模多年,一直被拒绝,因为额外的代码量必须是手-cranked。 EF由于设计人员和代码生成目前被视为可行的解决方案,因此任何涉及手动启动XML的选项只会使我的其余同事远离EF。我正在寻找可以使用设计师或使用代码来完成的任务。



编辑:



我想我正在寻找的是...



(a)创建没有EF的模型的方法引用列在商店(ssdl),因此不想以任何方式操纵它
(b)一种在创建ObjectContext时编程设置StoreGeneratedPattern属性与对象属性的方法(简单的答案是手动操作这个在.ssdl中,但是如果我从数据库刷新了模型,那么这将被覆盖,我不能在.csdl,.msl& .ssdl被手动启动的路径下)。

$ b $你可以用实体框架来做这个吗?是;这很容易。你可以用Entity Framework设计器来做这个吗?不幸的是,这是更难的。



您遇到的问题是列存在于EDMX中的存储架构(SSDL)中。使用GUI设计器删除列可以将其从客户机架构中删除,而不是映射或存储模式。但是,进入EDMX并删除它很简单。完成后,您还可以将其从EDMX的客户端架构部分的映射中删除,实体框架将更长时间地抱怨它未被映射。



问题解决,对吧?



当您使用GUI设计器从数据库更新EDMX时,存储模式将被丢弃并重新生成。所以你的专栏会回来。据我所知,没有办法告诉GUI设计师从不映射一个特定的列。因此,每次使用GUI设计器进行更新时,都必须重新执行此操作。幸运的是,EDMX是XML,所以您可以使用XML转换,LINQ或您选择的XML工具来执行此操作。


I have a database that I wish to build an EF model from, however I do not want to include certain columns from the database as the columns concerned are maintained exclusively on the server and should not be manipulated by any application.

Both of the columns are DateTime (if this makes any difference), one of the columns is nullable and is maintained by a trigger on updates and the other is not nullable and set using a default value in the table definition.

I guess I am looking for something like the "Server Generated" option in Linq2Sql; but I cannot find such an option.

Can anybody tell me how to work around this?

Caveat:

I have been trying to introduce business object modelling at my place of work for some years and it has always been rejected because of the amount of additional code that has to be hand-cranked. EF is currently being seen as a viable solution because of the designer and code generation therefore any option that involves hand-cranking the XML will only turn the rest of my colleagues away from EF. I am therefore looking for something that can be done either using the designer or using code.

EDIT:

I guess that what I am looking for here is either...

(a) a way to create the model without EF referencing the columns in the store (ssdl) and therefore not looking to manipulate it in any way (b) a way to programatically set the "StoreGeneratedPattern" attribute against the property when I create the ObjectContext (the easy answer is to manually manipulate this in the .ssdl, but this would then be overwritten if I refreshed the model from the database and I cannot go down the route where the .csdl, .msl & .ssdl are hand-cranked).

解决方案

Can you do this with the Entity Framework? Yes; it's easy. Can you do this with the Entity Framework designer? Unfortunately, that is much harder.

The problem you're having is that the column exists in the storage schema (SSDL) in your EDMX. Removing the column with the GUI designer simply removes it from the client schema, not the mapping or the storage schema. However, it's simple enough to go into the EDMX and remove it. Having done that, you can also remove it from the mapping in the client schema portions of the EDMX, and the entity framework will longer complain that it is unmapped.

Problem solved, right?

Well, no. When you use the GUI designer to update the EDMX from the database, the storage schema is thrown away and re-generated. So your column will come back. As far as I know, there is no way to tell the GUI designer to never map a particular column. So you will have to re-do this every time you update with the GUI designer. Fortunately, the EDMX is XML, so you can do this with a XML transform, LINQ, or the XML tool of your choice.

这篇关于实体框架:忽略列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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