Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:11:29+00:00 2026-06-12T00:11:29+00:00

I have read many people say that model types should not be exposed to

  • 0

I have read many people say that model types should not be exposed to View, but instead it should be wrapped inside ViewModel types. Is there some example where I can see how is synchronization done between ViewModel and Model data, using Entity Framework. In particular, I need an example of editable collection (example: DataGrid or DataForm displaying Customer list).

So, something like this

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

but with editable collection data, not just read-only.

An example of what I am interested in:

1) a grid needs to show products. 3 columns are shown:

  • Product Code
  • Product Name
  • Product Type
  • Product Unit

Type is an Enum, lets say: ProductType { TypeA, TypeB }. If you find difficult to implement enum, then have it a int, its not a problem. Important thing is that Type cannot be changed if Product was already used as reference in somce other table. I am using this rule, since you can not do this with attributes (Data annotations), and needs to be done on ViewModel side.

Each property on a product class must be bound to CustomerViewModel property. You can put some rules for Code and Name:

Code Unique
Name: Required, MaxLength(30)

EDIT: my main concern is how we do synchronization between ViewModel and EF Model in batch updates. An example would be:

1) when removing Product, if we remove it from ProductViewModel collection and DBContext, what happens when user decides to cancel (made a mistake or anything)? Do we need to reload all Product from database and recreate all ProductViewModels?

2) user changes product and puts product to invalid state (remember that invalid state is still acceptable for object unless we decide to flush it to database). Then user moves cursor to another product, changes it (this product remains in valid state), and execute save command. What should we do at that point?

I have my solutions for all these questions, but I am not sure if they are correct, are there better ways, so the reason why I asked for other opinions from people that are using this method in daily work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:11:30+00:00Added an answer on June 12, 2026 at 12:11 am

    If you need batch update ability then application needs some updates:

    1) Do not call SaveChanges from ProductViewModel. Changes should be saved only on pressing “Save” button

    2) In the MainViewModel you need to maintain two additional collections: for new items and deleted items. When user presses “Save” button deleted items will be removed from database and new items will be added to database.

    3) When user presses “Cancel” button all changes should be reverted. I.e. all fields should be restored to their original state, deleted items should be added back and new items should be deleted

    Please see update on github: https://github.com/alexshakurin/EditableDataGridMVVM

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read many questions about the facebook login but until not I didnt
I have read many questions about Android, J2ME and RecordStore , but I still
I have read so many times, here and everywhere on the net, that mutexes
Often, people say that it is better to say s.Length == 0 instead of
I know there have been many people asking the same thing, I have read
I have made list view with checkboxes. I have read similar articles n many
1) I have read many times about undefined behavior in C. That is about
I have read many people really like the MapMaker of Google Guava (Collections), however
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read many answers regarding this still i am getting confused if i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.