简体   繁体   English

实体框架 - 数据绑定自定义字段出错(未找到属性)

[英]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: 我在部分类中使用getset方法创建了一个名为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.

相关问题 c# ms access 数据库错误ColumnCount 属性不能在数据绑定的DataGridView 控件上设置 - c# ms access database error ColumnCount property cannot be set on a data-bound DataGridView control 未找到实体框架插入字段的错误 - Error on Entity framework insert field not found 无法在数据绑定的 DataGridView 控件上设置 ColumnCount 属性。 c# - ColumnCount property cannot be set on a data-bound DataGridView control. c# 使用 Mediator 从数据绑定属性设置器中调用异步方法 - Using Mediator to call async method from within a data-bound property setter 空白行在数据绑定的DataGrid中不可见 - Blank Row is not visible in data-bound DataGrid 在DataTemplate内部显示数据绑定StackPanel - Displaying a data-bound StackPanel inside a DataTemplate 数据绑定列表框和PropertyGrid的更新问题 - Update issue with data-bound Listbox and PropertyGrid ASPX服务器端数据绑定控件的DataSource属性的最低要求是什么? - What's the minimum requirement for the DataSource property of ASPX server side data-bound controls? 从异步方法填充数据绑定属性的最佳方法是什么? - What is the best way to populate a data-bound property from an async method? 如何在转发器内的数据绑定字段上进行客户端文本格式设置? - How can I do client side text formatting on a Data-bound field inside a repeater?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM