Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Earlier I asked a question about "Entity Framework and layer seperation" and found out that some people use a ViewModel to show their data in the UI.

For example if we got a "Person" table and an "Address" table and I want to show a combination of the two tables. I would then create a ViewModel, lets call it PersonAddress, and show it on the UI.

I have a few questions regarding this:

  1. How do you best save the "PersonAddress" object to the database since it does not exist in my EF context?
  2. If I only wanted to show the "Person" object, would I still create an identical "ViewModel" object to the one EF creates for me (without the EF specific properties ofcourse)
  3. Do people use the "EntityState" property and dosent it become useless if you only interact with the "ViewModel"?
share|improve this question

1 Answer 1

How do you best save the "PersonAddress" object to the database since it does not exist in my EF context?

You don't. You save a Person and the attached Address separately (in a transaction, if needed). Loading data and saving data can also be thought as different concerns (see CQRS).

If I only wanted to show the "Person" object, would I still create an identical "ViewModel" object to the one EF creates for me (without the EF specific properties of course)

Yes. Again, this is about separation of concerns.

Do people use the "EntityState" property and dosent it become useless if you only interact with the "ViewModel"?

Depends on what you are doing. A ViewModel models the UI, not your data.

share|improve this answer
    
Okay, thanks for the answers. About the 3rd one. What would my "PersonAddress" object look like? Im asking because i havent created one yet. I assume it would contain the properties I want to show along with the PersonId and AddressId for select/update/delete things. Then after ive selected and updated Person and Address with the new information, I can use the "EntityState" for whatever I like. Is this the right way to do it? –  Thomas Dec 22 '11 at 12:58
    
It pretty much would mirror the UI. It wouldn't have the Id fields if they are not displayed. I'd expect an Add/Update Person and Add/Update Address for person views to also exist and handle those specific actions. –  Oded Dec 22 '11 at 13:22
    
Hm, im not sure I understand. Lets say you show a list of "PersonAddress" and you want to update row no. 2. How do you know which Person to update in the db if you only have information like name and age in the ViewModel? I might need an example of some sort hehe –  Thomas Dec 22 '11 at 14:33
    
@Thomas - If the view requires the Id's, then they should be part of the ViewModel. –  Oded Dec 22 '11 at 14:34
    
+1 for A ViewModel models the UI, not your data –  Rachel Feb 6 '12 at 15:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.