支持自定义实体框架提供程序中的默认列值 [英] Supporting default column values in custom Entity Framework provider

查看:92
本文介绍了支持自定义实体框架提供程序中的默认列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自定义实体框架提供程序,我需要添加对该提供程序的默认列值的支持。当用户使用实体框架向导并选择包含具有默认值的列的表时,这些默认值未被填充到实体设计器中。

I'm working on a custom entity framework provider and I need to add support for default column values for this provider. When the user uses the entity framework wizard and selects a table that includes columns with default values, those default values are not being populated into the entity designer.

我是一个在这个人口应该发生的地方很少迷失。我相信适当的地方将在GetEdmType方法覆盖DbXmlEnabledProviderManifest,但我只是看不到如何设置默认值,如果这是正确的地方。

I'm a little lost on where exactly this population should take place. I believe the appropriate place would be in the GetEdmType method override of DbXmlEnabledProviderManifest but I just don't see how to set the default value, if this is the correct place.

任何人都有写EF支持的经验,支持表列的默认值?你如何实现这一点?

Anybody has experience writing EF providers that support default values for table columns? How do you implement this?

推荐答案

我有点晚了,但DbXmlEnabledProviderManifest不是正确的地方添加默认值。提供程序清单描述了数据库引擎本身的功能,并且与此数据库引擎是特定的(通用的),而不是给定的数据库和/或表。提供者清单中的默认值告诉EF如果用户不提供给定列属性,则使用什么值(例如,如果用户用户未指定小数列的缩放或精度来自提供商清单的值将用于此列的比例和/或精度)。
如果您只想为属性插入一个默认值,最简单的方法是将与实体列相对应的属性设置为构造函数中的该值。这样,用户可以始终将其设置为不同的值,但是如果他/她没有将默认值发送到数据库。对于某些角色情况,数据库中的某些列不具有实体对应的属性,您可以在SSDL中的Property元素上使用DefaultValue属性,该属性将在添加行时插入数据库。如果这些属性不可为空,这是非常有用的,因为没有告诉EF什么值应该插入EF会尝试插入null,这对于不可空的列显然是失败的。

I am a bit late to the party but DbXmlEnabledProviderManifest is not the right place for adding default values. The provider manifest describes capabilities of the database engine itself and is specific (and general) to this database engine and not to a given database and/or table. The default value in the provider manifest tells EF what value to use for the given column property if one is not provided by the user (e.g. if the user user does not specify scale or precision for a decimal column the value from provider manifest will be used for scale and/or precision used for this column). If you want just to insert a default value for a property the easiest way is to set the property that corresponds to the column on your entity to this value in the constructor. This way the user can always set it to a different value but if s/he does not the default value will be sent to the database. For some corner case scenarios where some of the columns in the database do not have corresponding properties on entities you can use DefaultValue attribute on the Property element in SSDL which will be inserted to the database when you add a row. This is especially useful if those properties are not nullable since without telling EF what value should be inserted EF would try inserting null which would obviously fail for non-nullable columns.

这篇关于支持自定义实体框架提供程序中的默认列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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