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

 

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;
        }

    }
}

 

data structures and algorithms
https://github.com/PacktPublishing/C-Sharp-Data-Structures-and-Algorithms
https://github.com/aalhour/C-Sharp-Algorithms
https://github.com/abdonkov/DSA
https://github.com/scottfalbo/data-structures-and-algorithms
https://github.com/anirban-s/data-structures-and-algorithms
https://github.com/akgmage/data-structures-and-algorithms
Collection of well known Data Structures, and Algorithms in Go, C++, Java & Python
https://github.com/phishman3579/java-algorithms-implementation
https://github.com/thepranaygupta/Data-Structures-and-Algorithms
https://kentdlee.github.io/CS2Plus/build/html/index.html
https://github.com/kentdlee/CS2Plus
https://github.com/YanxinHuang/data-structure-and-algorithm
https://www.codeproject.com/articles/16337/back-to-basics-generic-data-structures-and-algorit
https://github.com/krahets/hello-algo
《Hello 算法》一本動畫圖解、能運行、可提問的數據結構與算法入門書。提供 Java, C++, Python, Go, JS, TS, C# 源代碼。
https://github.com/topics/data-structures-and-algorithms

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章