0

I see a lot of this going on in MVC3 while using the Entity Framework but it looks like a LINQ to SQL model.

public class SomeClass {

    public int Id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
}

Why and when is this type of model necessary while using the EF? Everything I’ve ever needed to do in a controller while using the EF, I’ve done with EF as my model, isn’t that what the EF is for, so you no longer have to write L2S style models???

Am I getting confused thinking I’m looking a tutorial using the EF but it’s really using L2S or am I missing something???

Confused,

Mike.


Torm, you are a champ! And +1 to Jan as I read that first, it makes sense although I haven’t come across a scenario when I’ve had to do so, and no longer felt I was missing something.

But Torn has it. Code first, what the %&*$!

The first tutorial I did in MVC # something, I created a database, filled it with dummy data (that takes time) but it was a code first tutorial, I ran the app and all the dummy data was gone!!! I have no idea why there is a code first option, I always start an app with a database. I guess it’s for people who work in a large company and don’t have access to the database while writing MVC??? Even so why not build a dummy database for testing, I always do? I will ask that question in another post.

So all the model classes for a database first approach with EF are automatically generated in the Model/.Designer.cs file! And therefore you don’t need to write L2S style models at all, EF does it all for you.

I think I know everything there is to know about MVC now, not!

Cheers,

Mike.

Kev
  • 118,037
  • 53
  • 300
  • 385

2 Answers2

3

I think you are talking about Viewmodels.

When pushing data from the controller to a view, your model classes from your service layer might not fit exactly to the needs of your views.

MVC is a pure GUI framework, so the M in MVC stands for the model for the view, not necessarily for the model from your persistance or service layer.

Its absolutely common to define separate model classes like the one you have shown and map the classes from your EF or L2S context to the GUIs model classes.

Jan
  • 15,802
  • 5
  • 35
  • 59
  • well looking at the "Id" parameter I don't think it's about ViewModels but maybe I'm wrong :) – Paweł Staniec Nov 29 '11 at 08:22
  • @torm: Think about an edit view for that model. The id should definitly be included in your view (as a hidden field). Otherwise it would be hard to find that record again. – Jan Nov 29 '11 at 08:24
  • Sure, however it's not that common in tutorial apps ;) I would rather see ViewModel being used as DTO with classes defined in your model. Let's look at a template of AccountModel included in MVC3 templates. RegisterModel etc. are these models or ViewModels ? – Paweł Staniec Nov 29 '11 at 08:32
  • @torm: `RegisterModel` is solely used to transfer data between the view and the Register action in the template. So its the classical model (The M of MVC). But i havn't wrote, that you shouldn't use the model classes from your service/persistance layer as the model for your view. I kust have answered the question, why there is another set of similar looking classes in the MVC app. – Jan Nov 29 '11 at 08:39
  • yeah, that's fine, I just wrote that I don;t think it's a ViewModel :) that's it :) – Paweł Staniec Nov 29 '11 at 08:44
1

There are two main approaches to deal with your model in Asp.Net MVC applications. Database first and Code first. Here is a good read Code-first vs Model/Database-first

Database first assumes you will be creating your model on the DB level and then importing it to your application. Then in you applications you don't see the model classes as they are generated for you automatically.

In code first you start with Plain Old Clr Objects (classes) that define your model. Once you have these classes you map them in DataContext class that tells the EF how to organize the objects into collections (sets). Once you run your app the EF creates a database for you.

Community
  • 1
  • 1
Paweł Staniec
  • 3,151
  • 3
  • 29
  • 41