Nicolas
Nicolas

Reputation: 2376

WPF - NotMapped annotation in EF6 not storing / saving property value(s)

When I try to insert or update data in my WPF usercontrol datagrid, the data is not being saved to the corresponding property. This is being caused by (at least so I believe) my bound property having the [NotMapped] attribute, all the other properties without the annotation are working correctly.

The data which need's to be updated is inside a DataGrid component which is bound to an ObservableCollection with the appropriate model. Inside the model there are several properties with the [NotMapped] annotation, these properties should be excluded in the creation of the table (model) in my database, though I do need them for binding input, hence the use of the [NotMapped] annotation.

Since the data is bound to a ObservableCollection I can't add the [NotMapped] properties to the usercontrol directly (so they wont be a part of the model).

Below an example:

Part of the XAML In the image below we can see 1 property (pBreedte) which is 1 of the NotMapped properties, as well as the itemsource of the datagrid: enter image description here

UserControl Code behind (part of it) enter image description here

Part of the model which is used in the ObservableCollection

The model is being used for EF6 (code first). enter image description here

Is there any way that the NotMapped property values can be stored / saved? The easiest would be to just include the NotMapped properties in the database (so removing the annotation completely) but I am trying to avoid this.

More background information

The NotMapped values are added because they function as a placeholder property. Initially I had several decimal properties bound to the datagrid directly, but textboxes can't handle decimal values very well (WPF validation rule preventing decimal entry in textbox?). So I created a string placeholder for those decimal properties, when the form is being saved the decimal properties are being set to their string placeholder counterparts. This way the user can add decimal places without having to use a delay, value converter or anything.

Upvotes: 1

Views: 751

Answers (1)

DasSoftware
DasSoftware

Reputation: 974

If you don't need that information in your database, then don't store it - meaning your approach is good.

What I think here what might be the problem is that you are using your entity/database model as UI model.

I would suggest that you try to introduce a different model for the UI controls and user input. The models might seem to be duplicate at the beginning but while you are working on your application they will start to differ, but still describing the same items just form different perspectives.

Example:

Entity model has a class CarEntity. It is a pure POCO class, having only the needed properties that will contain the data in the corresponding table.

Ui model has a class CarUi. It has the same properties as the CarEntity. They are loaded and mapped from the database (from the CarEntity) shown to the user. If the user changes something, the diff values are mapped from the CarUi to the CarEntity and then stored to the DB.

With this separation of models approach, you should not face the issue where one constraint (mark column not to be stored in a table) influences other functionality.

Hope this helps, Cheers and happy coding!

Upvotes: 2

Related Questions