Reputation:
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.
Upvotes: 0
Views: 489
Reputation: 3181
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.
Upvotes: 1
Reputation: 16048
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.
Upvotes: 3