实体框架、未映射的属性和动态数据
我正在使用实体框架数据模型来驱动动态数据网站,供用户用来更新数据。
其中一个实体包含一个不可为空的字符串属性(描述)。在数据库中,其中一行的描述为空(不是 null,而是空字符串)。当我尝试更新描述时,出现以下验证错误:“此属性不能设置为空值”。 如果我手动更新数据库中的描述,然后编辑属性,它将按预期工作。但是,一旦我将数据库中的描述更改回空字符串,就会发生验证错误。该错误发生在描述的设置器上。
因此,我尝试添加一个名为 CustomDescription 的附加字符串属性,它基本上包装了 Description,使 Description 在实体的元数据中成为 ScaffoldColumn(false),并将新属性添加到实体的元数据中。
[ScaffoldColumn(true)]
public string CustomDescription
{
get { return this.Description; }
set {
if (value == null)
{
value = string.Empty;
}
this.Description = value;
}
}
但是,我需要向此属性添加什么才能使其显示在动态数据站点上?
I'm using an Entity Framework data model to drive a Dynamic Data website for use by users to update data.
One of the entities contains a non-nullable string property (Description). In the database, one of the rows has an empty Description (not null but an empty string). When I try to update the Description I get the following validation error: "This property cannot be set to a null value".
If I manually update the Description in the database and then edit the property, it works as expected. But as soon as I change the Description in the database back to an empty string, the validation error occurs. The error happens on Description's setter.
So I've tried adding an additional string property called CustomDescription which basically wraps Description, made Description a ScaffoldColumn(false) in the entity's metadata and added the new property to the entity's metadata.
[ScaffoldColumn(true)]
public string CustomDescription
{
get { return this.Description; }
set {
if (value == null)
{
value = string.Empty;
}
this.Description = value;
}
}
However what do I need to add to this property in order to get it to display on the dynamic data site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是非空字段中的旧值是空字符串。
默认情况下,框架将其转换为 null。
要修复该错误,只需将以下属性添加到您的字段中:
Problem is that old value was empty string in Non-Nullable field.
By default framework is converting it to null.
To fix the error just add the following attribute to your field:
在相应的 Metadata 类中,只需像引用实际字段一样引用它:
In the corresponding Metadata class, just refernce it as you would an actual field: