tags:

views:

50

answers:

2

Hi,

I am busy with an MVC 2 project. I have my entity framework 4 .edmx file in a different project and referenced into my web application. I'm not too sure when I will create model files in my Models directory (created by the MVC application) if my .edmx file is in a separate project.

Please can someone help on this confusion. I would appreciate some samples regarding my scenario.

Thanks.

+3  A: 

You could place your View Models into this directory.

Darin Dimitrov
Yes this would be the case if I were to create my .edmx file in the models directory, but I did this in a separate project. Am I missing something here?
Brendan Vogt
No, you are not missing anything. Leave your models in the separate project and put your view models in the Models folder.
Darin Dimitrov
+3  A: 

You don't have to put your Models in the 'Models' folder but it is just a best practice that the framework has suggested. I personally have ONLY my view models in the 'Models' directory and have one for each view. I then map my models/objects from my ORM to these models so that the objects passed back from my data access layer are not the ones I am passing to my views. Normally my DAL classes don't map 1 to 1 to my view models.

Kelsey
Would I need to do this if my .edmx file is in a separate project? If it is in a separate project, would there still be cases when I would add code to the Models direcotry? Could you maybe give me an example of such a scenario?
Brendan Vogt
@bioluminescence Normally the mapping from my ORM objects to my view models happen in the controller. So I include a reference in my project to my DAL which contains all my entity framework code. Then in a specific action you call the DAL and get an object back as your entity framework objects. Then the code maps the data to the view model objects and passes it to the view. All your entity framework code should be wrapped up in a seperate DLL that you just include in your MVC project. You could take it one step further and include a repository pattern to abstract the ORM objects in your DAL.
Kelsey
@Kelsey: When you refer to "You could take it one step further and include a repository pattern to abstract the ORM objects in your DAL" you mean? What I did was to use the POCO entity generator that extracts the classes and the entity context. Is this what you mean?
Brendan Vogt