ARTICLE

ASP.NET MVC 3 Database Connectivity Using Entity Framework

Posted by Raman Ghantiyala Articles | ASP.NET MVC with C# September 06, 2012
In this article you will learn how to use ASP.NET MVC 3 Database Connectivity using the Entity Framework.
Reader Level:
Download Files:
 

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.
 

Login to add your contents and source code to this article
post comment
     

Thanks Raman, this is a useful article

Posted by Isman Subarkah Apr 29, 2013

Thanks Dinesh..

Posted by Raman Ghantiyala Sep 11, 2012

Good start Raman.

Posted by Dinesh Beniwal Sep 10, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.