Complete Insert/Update/Delete of Child Entities in Entity Framework

后端 未结 2 1577
广开言路
广开言路 2020-12-09 21:41

I know it has been asked before, but after long hours of searching and coding I can\'t get to a working and clean approach. Here is what I have:

public class         


        
2条回答
  •  轮回少年
    2020-12-09 22:27

    This is just proof of concept.

    Controler have func UpdateModel but it wont work with more complex model which have included child records. Look for TestUpdate.

    Rule#1: Every Table have PK Id column.

    Rule#2: Every FK have to be set.

    Rule#3: Cascade delete need to be set. if you want remove related record.

    Rule#4: New Record need to have Id = 0 or better will be Null but Id cant be null.

    public class TestController : Controller where T : class
    {
        const string PK = "Id";
    
        protected Models.Entities con;
        protected System.Data.Entity.DbSet model;
        public TestController()
        {
            con = new Models.Entities();
            model = con.Set();
        }
    
        // GET: Default
        public virtual ActionResult Index()
        {
            ViewBag.Result = TempData["Result"];
            TempData["Result"] = null;
    
            var list = model.ToList();
            return View(list);
        }
    
        [HttpGet]
        public virtual ActionResult AddEdit(string id)
        {
            int nId = 0;
            int.TryParse(id, out nId);
    
            var item = model.Find(nId);
            return View(item);
    
        }
    
        [HttpPost]
        public virtual ActionResult AddEdit(T item)
        {
            TestUpdate(item);
    
            con.SaveChanges();
    
            return RedirectToAction("Index");
        }
    
        [HttpGet]
        public virtual ActionResult Remove(string id)
        {
            int nId = 0;
            int.TryParse(id, out nId);
            if (nId != 0)
            {
                var item = model.Find(nId);
                con.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                con.SaveChanges();
            }
            return Redirect(Request.UrlReferrer.ToString());
        }
    
        private void TestUpdate(object item)
        {
            var props = item.GetType().GetProperties();
            foreach (var prop in props)
            {
                object value = prop.GetValue(item);
                if (prop.PropertyType.IsInterface && value != null)
                {
                    foreach (var iItem in (System.Collections.IEnumerable)value)
                    {
                        TestUpdate(iItem);
                    }
                }
            }
    
            int id = (int)item.GetType().GetProperty(PK).GetValue(item);
            if (id == 0)
            {
                con.Entry(item).State = System.Data.Entity.EntityState.Added;
            }
            else
            {
                con.Entry(item).State = System.Data.Entity.EntityState.Modified;
            }
    
        }
    
    }
    

    Here is project https://github.com/mertuarez/AspMVC_EF/

    You need to create Controler for model and create view for action. In case of AddEdit action you have to create editor Template for subtypes.

提交回复
热议问题