using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Runtime.Remoting.Contexts; using System.Text; using System.Threading.Tasks; using DuEntity; using DuInterface; using DuUtilitieDB; namespace DuDAL { /// <summary> /// geovindu,Geovin Du 涂聚文 sql server /// EntityFramework 6.0 /// </summary> public class CategoryDAL: ICategory { /// <summary> /// 添加 /// </summary> /// <param name="category"></param> /// <returns></returns> public int Add(Category category) { int addok = 0; try { using (var context = new DuDbContext()) { //context.Database.ExecuteSqlCommand(""); context.Categories.Add(category); addok = context.SaveChanges(); } } catch (Exception ex) { ex.Message.ToString(); } return addok; } /// <summary> /// 修改 /// </summary> /// <param name="category"></param> /// <returns></returns> public int Update(Category category) { int edidok = 0; try { using (var context = new DuDbContext()) { // edidok = ctx.Database.ExecuteSqlCommand(""); context.Entry(category).State = category.CategoryId == 0 ? EntityState.Added : EntityState.Modified; edidok = context.SaveChanges(); } } catch (Exception ex) { ex.Message.ToString(); } return edidok; } /// <summary> /// 删除 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public int Delte(int categoryId) { int delok = 0; try { using (var context = new DuDbContext()) { // delok = ctx.Database.ExecuteSqlCommand(""); var dellist=context.Categories.Where(c => c.CategoryId == categoryId).ToList(); if(dellist.Count > 0) { context.Categories.RemoveRange(dellist); delok = context.SaveChanges(); // context.Categories.Where(x => x.CategoryId == categoryId).DefaultIfEmpty(); //context.Categories.Where(x => x.CategoryId == categoryId).DeleteFromQuery(); } } } catch (Exception ex) { ex.Message.ToString(); } return delok; } /// <summary> /// 查找一个实例 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public Category SelectInfo(int categoryId) { Category category= null; try { using (var context = new DuDbContext()) { // delok = ctx.Database.ExecuteSqlCommand(""); //category = context.Categories.Where(c => c.CategoryId == categoryId); category = context.Categories.FindAsync(categoryId).Result; } } catch (Exception ex) { ex.Message.ToString(); } return category; } } }
标签:category,donet,Edit,Entity,int,ex,context,using,categoryId From: https://www.cnblogs.com/geovindu/p/17068186.html