0

I'm using MVC 4 and EF and I'm new to both. I've created an entity named TimesheetModel from an existing DB.

Question: When creating a controller (using the EF framework template) I have several options available to me under both "Model Class" and "Data Context class" dropdowns. By convention, would one usually create a controller per each model you needed to access? And the same for data-context? Also I'm not 100% sure on the data-context portion either.

I've gone through a few tutorials this morning on the topics but they seem to shy away from explaining the "why" you select what you select in those fields. Halp meh! Thx

1
  • Hm. Maybe a better question would be, using EF and creating a .edmx, does one need to create a Model that relates to said .edmx? To correlate data items to your code? I'm noticing in a few tutorials some are doing this to create a class that extends DbContext.. does that make my original question more clear? Commented Aug 14, 2015 at 16:40

2 Answers 2

2

In the Model Class, you select the class which you want EF to generate its Actions.

In the Data Context class, you type in the "Context" class in which all classes are declared. The context usually is the class which is the main where all starts exactly. If u used code first, you first create Context class, which shows which other classes are creating under it. Otherwise, if u do Database First, then the context is the name of the database usually.

Hope I could explain so you could understand.

Sign up to request clarification or add additional context in comments.

Comments

1

The data context represents your database. Usually, in small application, there is only one.

The Model represents an entity, that almost always represents a table in database.

By convention, would one usually create a controller per each model you needed to access?

It depends on the model. If you have a User class, you usually need to have a UsersController, to create, update and delete an user. If you have a UserRole class, you don't need an UserRoleController because it has a relation with User and you can use UsersController to handle UserRole too.

Keep in mind that there is no rule for that. You can do whatever you want. The Scaffolding of the MVC is just for teaching proposes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.