public bool Add() { if (this.Valid()) { this.Id = General.UniqueString(this.Id); using (WMContext context = new WMContext()) { GoodCategories model = new GoodCategories { Id = this.Id, ParentId = this.ParentId, Name = this.Name, Level = 1, Sort = 0 }; GoodCategories level = context.GoodCategories.Find(this.ParentId); if (level == null) { model.Level = 1; } else { model.Level = level.Level + 1; } var sort = context.GoodCategories.Where(gc => gc.ParentId.Equals(this.ParentId)).ToList(); if (General.IsNullable(sort)) { model.Sort = 1; } else { model.Sort = sort.Max(gc => gc.Sort) + 1; } context.GoodCategories.Add(model); context.SaveChanges(); } return(true); } return(false); }
public bool Update() { if (this.Valid(true)) { using (WMContext context = new WMContext()) { GoodCategories model = context.GoodCategories.Find(this.Id); if (model != null) { model.ParentId = this.ParentId; model.Name = this.Name; model.Level = this.Level; model.Sort = this.Sort; context.SaveChanges(); return(true); } } } return(false); }