Search code examples
model-view-controllerc#-4.0webapi

How do I configure a (MVC using entity framework) controller in a web API project


I have a MVC 4.0 Web API project and I have already added the model of my database to it so I have the files:

.edmx

.Context.tt

.context.cs

.edmx.diagram

I'm trying to figure out how to add a controller after I've created my entity framework model.

It's asking for the Model Class and Data context class.

I'm not sure what they are in my solution.

Can anyone give me a hand?


Solution

  • It depends what you are trying to do... 1. you can create an empty controller and then you do not need to select the Model and Context. you write the Add/Edit/Delete functions yourself and their views. 2. you can create a controller with read/write action and views - this way you get these functions Add/Edit/Delete + views done for you automatically:).

    The Context is (as I understand it) the reference to the database. if you havent already you should add your model classes that you want to create as tables in the database to the YOUR-PROJECTNAMEContext.cs file under DAL folder. this will create the tables for you (if I didn't forget anything). you shoula add somethis like:

    public DbSet<Note> Notes { get; set; }
    

    where Note is your class for example and Notes is your table name.

    now if you want to create a Controller for you notes class in the model and let MVC to do for you the ADD/Edite/Delete function and views than your model is: Note and Context is YOUR-PROJECTNAMEContext.

    Hope it will help.