[英]Entity Framework - Error on Data-Bound Custom Field (Property Not Found)
Using Entity Framework, I've extended an entity class with a "display-friendly" version of a property, in this case, CommissionRate
which is an entity property and CommissionRateDisplay
which is my own custom property. 使用实体框架,我扩展了一个实体类,其中包含一个属性的“显示友好”版本,在本例中,
CommissionRate
是一个实体属性, CommissionRateDisplay
是我自己的自定义属性。 In my bound Web Forms control, I want to display an integer percent 3 but in the database I want to store as a decimal 0.03 . 在我绑定的Web窗体控件中,我想显示一个整数百分比3,但在数据库中我想存储为小数0.03 。
I've created a custom field in the partial class called CommissionRateDisplay
with a get
and a set
method, as below: 我在部分类中使用
get
和set
方法创建了一个名为CommissionRateDisplay
的自定义字段,如下所示:
partial class SalesOrder
{
public double CommissionRateDisplay
{
get { return (this.CommissionRate ?? 0) * 100; }
set { this.CommissionRate = value / 100; }
}
}
When saving the entity, I'm presented with the following error: 保存实体时,我遇到以下错误:
A property named 'CommissionRateDisplay' was not found on the entity during an insert, update, or delete operation. Check to ensure that properties specified as binding expressions are available to the data source.
I'd simply like to bind my custom property and have it persist the modified original in the database. 我只想绑定我的自定义属性并让它在数据库中保留修改后的原始属性。
get
ting the value works just fine, but set
ting does not. get
的价值工作得很好,但set
不是。
Am I approaching this in the right way, or is there a better way? 我是以正确的方式接近这个,还是有更好的方法? Apologies if this is a duplicate;
如果这是重复的,请道歉; I could not find a solution that clearly and succinctly solved the problem.
我找不到一个明确而简洁地解决问题的解决方案。
您需要向自定义属性添加[NotMapped]
注释,以使Entity Framework知道您不打算将其作为表中的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.