首页 > 其他分享 >CSharp: Add,Edit,Del,Select in donet using Entity Framework

CSharp: Add,Edit,Del,Select in donet using Entity Framework

时间:2023-01-26 20:44:06浏览次数:58  
标签:category donet Edit Entity int ex context using categoryId

 

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

相关文章

  • Visual Web Developer 2005 Express Edition下载安装
    VisualWebDeveloper2005ExpressEdition下载安装2007年05月18日11:11该软件为微软的WEB开发工具的免费版软件介绍:​​​http://www.microsoft.com/china/msdn/expres......
  • POJ--2431 Expedition(优先队列)
    记录0:172023-1-26http://poj.org/problem?id=2431reference:《挑战程序设计竞赛(第2版)》2.2.4p77DescriptionAgroupofcowsgrabbedatruckandventuredona......
  • CSharp: Lazy Load Pattern in donet core 6
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceGeovin.Du.DuLazyLoad.DuGhost......
  • 第10章 带有依赖注入的服务配置(ASP.NET Core in Action, 2nd Edition)
    第2部分构建完整的应用程序我们在第一部分中讨论了很多内容。您看到了ASP.NETCore应用程序是如何由中间件组成的,我们主要关注RazorPages框架。您了解了如何使用Razor语......
  • 摘抄重要内容 Minimap2 On the definition of sequence identity
     HengLi'sblogArchiveCategoriesPagesTagsOnthedefinitionofsequenceidentity25November2018Sequenceidentityisawaytomeasurethe......
  • ofd-editor ofd文档在线编辑器
    ofd文件作为一种版式文档,要求显示效果完全固定、一致,导致很多人习惯性的忽略了ofd文件本身的编辑需求,导致现在缺乏一种基于浏览器的所见即所得的编辑ofd文件的方法。......
  • form-editor 一种基于虚拟DOM的动态表单设计器
    项目地址github: https://github.com/xjf7711/form-editor项目环境1、全局和本地安装TypeScript2、初始化、安装webpack、webpack-cli、webpack-dev-server、webpack-......
  • ASP.NET Core 实战-12.使用 Entity Framework Core 保存数据
    介绍EntityFrameworkCore数据库访问代码在Web应用程序中无处不在。无论您是构建电子商务应用程序、博客还是NextBigThing™,您都可能需要与数据库进行交互。不幸......
  • IdentityServer4入门
    IdentifyServer项目IdentityServer4是用于ASP.NETCore的OpenIDConnect和OAuth2.0框架。官网:​​https://identityserver4.readthedocs.io/en/latest/​​创建Asp.netWeb......
  • ASP.NETCore中的Identity
    标识(Identity)框架1、标识(Identity)框架:采用基于角色的访问控制(Role-BasedAccessControl,简称RBAC)策略,内置了对用户、角色等表的管理以及相应的接口,支持外部登录、2F......