Then create a new project and right click on model folder and select ado.net entity data model
And then And next and give sever and user name and password and select your database .Then your entity framework will be generated. It will look like this
For data annotation we have to add another class in model we cannot add data annotation in generated models class because when we update our entity framework our important validation will be loss to recover from this problem we will create meta class in the models folder.
And add following code
using System; using
System.Collections.Generic; using System.Linq; using System.Web; using
System.ComponentModel.DataAnnotations; using
System.ComponentModel.DataAnnotations.Schema; namespace TestSol.Models { publicclassRegistrationFormMeta { publicint Id { get; set; } [Required(ErrorMessage="First Name
is require!!")] publicstring FName { get; set; } publicstring MName { get; set; } publicstring LName { get; set; } publicDateTime Dob { get; set; } publicstring Mobile { get; set; } publicstring City { get; set; } [Required(ErrorMessage="pin is
require!!")] [RegularExpression("^[0-9]{6}$" , ErrorMessage = "Pin must be
Numeric and <=6")] publicstring Pin { get; set; } publicstring Address { get; set; } } publicclassCountryListMeta { publicint Id { get; set; } publicstring Country { get; set; } } }
Meta classes contain all the validation attribute to associate this class to metadata add a class in the models folder and name it Partial
using System; using
System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace TestSol.Models { [MetadataType(typeof(RegistrationFormMeta))] publicpartialclassRegistrationForm { } [MetadataType(typeof(CountryListMeta))] publicpartialclassCountryList { } }
Now I will use repository design pattern for all the curd operation. In repository we move all data access code into one place. Now I will create IRepository class in the models folder
using System; using
System.Collections.Generic; using System.Linq; using System.Web; using TestSol.Models; namespace TestSol.Models { publicinterfaceIRepository<T> where T:class { IEnumerable<T> SelectAll(); T SelectByID(object id); void Insert(T obj); void Update(T obj); void Delete(object Id); } }
This is an interface so for implementation we will create a class in the models folder and will implement this interface into that
using System; using
System.Collections.Generic; using System.Linq; using System.Web; using TestSol.Models; using System.Data.Entity; using System.Data; namespace TestSol.Models { publicclassRepository<T>:IRepository<T> where T:class { privateDemoEntities db = null; privateDbSet<T> table = null; public Repository() { this.db = newDemoEntities(); table = db.Set<T>(); } public Repository(DemoEntities db) { this.db = db; table = db.Set<T>(); } publicIEnumerable<T> SelectAll() { return table.ToList(); } public T SelectByID(object id) { return table.Find(id); } publicvoid Insert(T obj) { table.Add(obj); db.SaveChanges(); } publicvoid Update(T obj) { table.Attach(obj); db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); } publicvoid Delete(object Id) { T existing = table.Find(Id); table.Remove(existing); db.SaveChanges(); } } }
Add a new class in the models folder for setting the property for repository
using System; using
System.Collections.Generic; using System.Linq; using System.Web; namespace TestSol.Models { publicclassDML { publicIRepository<RegistrationForm>
RegistrationForm { get { returnnewRepository<RegistrationForm>(); } } publicIRepository<CountryList> CountryList { get { returnnewRepository<CountryList>(); } } } }
Now we will create unit of work for working with all the model classes.
using TestSol.Models; using System; using
System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace TestSol.Controllers { publicclassBaseController : Controller { // // GET: /Base/ protectedDML Uow { get; set; } privateDemoEntities db = null; public BaseController() { Uow = newDML(); } } }
Now add a home controller and inherit base controller in it. In the home controller I have create add, edit, delete and Update action
When we click edit It will return partial Userlist in the pop up .Code for Userlist
<linkhref="~/Content/bootstrap.css"rel="stylesheet"/> <scriptsrc="~/Scripts/bootstrap.js"></script> <linkhref="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css"rel="stylesheet"/> <divclass="table
table-responsive table-condense"> <tableclass="table table-bordered"> <tr> <th>First Name </th> <th>Middle Name </th> <th>Last Name </th> <th>DOB </th> <th>Mobile </th> </tr> @foreach (var d in ViewBag.Users asList<TestSol.Models.RegistrationForm>) { <tr> <td>@d.FName</td> <td>@d.MName</td> <td>@d.LName</td> <td>@d.Mobile</td> <td>@Ajax.ActionLink("Select", "Edit", new { Id = d.Id }, newAjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", OnSuccess = "filldata"// <-- HTTP
method }) </td> </tr> } </table> </div>
Thank you..
Liked By
Write Answer
How to Add Update Delete using entity framework
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
Vijay Saklani
27-Mar-2017Manish Kumar
01-Feb-2017I am providing you the solution of your question. Using entity framework and repository pattern
First create a demo database and create table RegistrationForm
And CountryList table and Insert record
Then create a new project and right click on model folder and select ado.net entity data model
And then And next and give sever and user name and password and select your database .Then your entity framework will be generated. It will look like this
For data annotation we have to add another class in model we cannot add data annotation in generated models class because when we update our entity framework our important validation will be loss to recover from this problem we will create meta class in the models folder.
And add following code
Meta classes contain all the validation attribute to associate this class to metadata add a class in the models folder and name it Partial
This is an interface so for implementation we will create a class in the models folder and will implement this interface into that
Now we will create unit of work for working with all the model classes.
Now add a home controller and inherit base controller in it. In the home controller I have create add, edit, delete and Update action
Our index view code in this I have added bootstrap library for better look and ajax validation
our partial view list code is
When we run our program it will look like this. From search text box we can filter our records.
When we click add then it will open in modal pop up it will look like this and it is from Add Partial View
And code for Add Partial
Thank you..