Monday 3 September 2012

Asp.net MVC 3 Database Connectivity using Entity framework.



Asp.net MVC 3 Database Connectivity using Entity framework.

First open the VS 2010.

After this
 
Database Structure???  Create registration table in database.



Now add the New Entity Model.


Next Step


Next Step


Now click on new connection now choose Microsoft Sql Server.

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





After this
Select the Table name.


Now click on finish button.


Now create the new controller.. Right click on controller and add new controller and type the controller testcontroller. While creating controller choose controllerwithempyt read/write mode.
[Note:-if we will create the test controller then in views test folder automatically will be generated.]
 

Now first build the solution….. For refreshing the model
Click on testcontroller look likes 



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


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:



Same as for create new view ,edit view ,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


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.

1 comment: