Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I'm using WPF with the MVVM pattern and Entity Framework to access data from a SQL database.
I use Entity Framework to reverse engineer the database and get entities from it.
Now I need to update some data on Table_A, which has 30 columns, but I only need the user to be able to update Column10. The problem is that Column12 depends on Column10 (Column12 = Column10 + Column11).

1 - What's the most efficient way to go? Do I have to retrieve all the columns from table A using a LINQ query? Should I create myself a new Entity in the .edmx designer? I do need the result to be an ObservableColletion so creating Anonymous queries is somewhat out of the picture..
2 - How and where would I code the piece of logic to update Column12?

What I have tried:

Getting all columns from the database, but it's not efficient..
Posted
Updated 21-Oct-16 11:20am

1 solution

Update selected fields:

You can add a new entity with only the required columns. Make sure that this partial entity is not used to add new records, unless all other columns are optional. I can imagine this entity has a key and three columns (10, 11 and 12), where you alter column10 and calculate column12.

Alternative is as described here: sql - How to update only one field using Entity Framework? - Stack Overflow[^]

Column12:

Since the fields in the entity are properties you can return the calculated value instead of the current value. Do not remove set. I must admit that I didn't test this, but this should work.
C#
public string colum12 { get { return column10 + column11;} set {} }

But why update it in code anyway? The same thing can be done in the database. I suggest you change column12 into a computed column: Specify Computed Columns in a Table[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900