ASP.NET MVC 3 Database Connectivity Using Entity Framework

First open VS 2010 and create an ASP.Net project as in the following:


open-asp.net-mvc.jpg

After this:

new-asp.net-mvc-project.jpg

Database Structure? Create a registration table in the database.

registration-table-in-database.jpg

Now add the new Entity Model, as in:

add-the-new-Entity-Model.jpg

Next Step:

open-data-model-wizard.jpg

Next Step

entity-data-model-wizard.jpg

Now click on new connection; now choose Microsoft SQL Server.

new-connection-Microsoft-Sql-Server.jpg

Continue. Select the database that you have created in Database.

connection-properties-sql-server.jpg


choose-database-connection.jpg


After this,

select the table name.

table-database-connection.jpg

Now click on the Finish button.
finish-button-database-connection.jpg

Now create the new controller. Right-click on controller and add a new controller and type the controller testcontroller. While creating the controller choose controllerwithempyt read/write mode.

[Note: If we create the test controller then the views test folder will be automatically generated.]
add-controller-asp.net-mvc.jpg
Now, first build the solution for refreshing the model.

Click on testcontroller looks like:

test-controller-asp.net-mvc.jpg

Right-click on index >>Add view >> choose strong type view >> Choose Model class >> LIST

add-view-asp.net-mvc.jpg

Then in Views folder >> Test folder >> index.chtml will be created.

List

Coding

private mvcEntities _db = new mvcEntities();

 

  public ActionResult Index()

   {

       return View(_db.registations.ToList());

   }

Output

output-asp.net-mvc-application.jpg

The same as for create new view, edit view and delete view.

Create new

  // GET: /work/Create

 

        public ActionResult Create()

        {

            return View();

        }

 

        //

        // POST: /work/Create

 

        [HttpPost]

        public ActionResult Create(registation model)

        {

            try

            {

                if (!ModelState.IsValid)

 

                    return View();

 

                _db.AddToregistation(model);

              

 

                _db.SaveChanges();

 

                return RedirectToAction("Index");

 

                // TODO: Add insert logic here

 

             

            }

            catch

            {

                return View();

            }

        }


Output screen

screen-output-asp.net-mvc.jpg

Edit View: Right-click on edit View >> ADD View >> strong type >>Edit

public ActionResult Edit(int id)

{

    var RecordToEdit = (from m in _db.registation

 

                       where m.id == id

 

                       select m).First();

 

    return View(RecordToEdit);

   

}

 

//

// POST: /work/Edit/5

 

[HttpPost]

public ActionResult Edit(int id,registation regtoedit)

{

    try

    {

        // TODO: Add update logic here

 

        var originalMovie = (from m in _db.registation

 

                             where m.id == regtoedit.id

 

                             //select m).First();

                             select m).First();

 

        if (!ModelState.IsValid)

 

            return View(originalMovie);

 

        _db.ApplyPropertyChanges(originalMovie.EntityKey.EntitySetName, regtoedit);

 

 

        _db.SaveChanges();

 

        return RedirectToAction("Index");

 

    }

    catch

    {

        return View();

    }

}

 

//


Delete View:
Right-click on edit View >> ADD View >> Partial View >>Delete

 

  // GET: /work/Delete/5

 

        public ActionResult Delete(int id)

        {

             var originalMovie = (from m in _db.registation

 

                                 where m.id == id

 

                                 select m).First();

 

            _db.DeleteObject(originalMovie);

            _db.SaveChanges();

 

            return RedirectToAction("Index");

        }

 

        //

        // POST: /work/Delete/5

 

        [HttpPost]

        public ActionResult Delete(int id,registation model)

        {

            try

            {

                var originalMovie = (from m in _db.registation

 

                                     where m.id == id

 

                                     select m).First();

               

                _db.DeleteObject(originalMovie);

                _db.SaveChanges();

 

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

    }

Now build the solution.

Thanks Happy Coding.
 

Up Next
    Ebook Download
    View all
    Learn
    View all