EF的四种增删改查方式

深渊向深渊呼唤

EF的四种增删改查方式

一,实例 数据库创建存储过程 实例展示

一,实例

数据库创建存储过程

--修改
create proc cp_update_role
(
@id int,
@name varchar(50),
@remark varchar(200)
)
as 
update roles set name=@name,remark=@remark where id=@id

select * from roles

--删除
create proc cp_delete_role
(
@id int
)
as 
delete roles where id=@id

--查询
create proc cp_select_role
(
@name varchar(50)
)
as 
select * from roles where name like'%'+@name+'%'

实例展示

修改的四种方式----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RbacDBEntities db = new RbacDBEntities()) {
                //方式一
                var role = db.Roles.Find(1);
                role.Name = "方式一";
                role.Remark = "备注一";
                db.SaveChanges();
                //方式二
                Role role2 = new Role()
                {
                    ID=2,
                    Name="方式二",
                    Remark="备注二"
                };
                db.Entry(role2).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                //方式三
                string role3 = @"update roles set name='方式三',remark='备注三' where ;
                db.Database.ExecuteSqlCommand(role3);
                //方式四
                db.cp_update_role(4,"方式四","备注四");
                Console.Read();
            } 
        }
    }
}





删除的四种方式----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RbacDBEntities db = new RbacDBEntities()) {
                //方式一
                var role = db.Roles.Find(1);
                db.Roles.Remove(role);
                db.SaveChanges();
                //方式二
                Role role2 = new Role()
                {
                    ID=5
                };
                db.Entry(role2).State = System.Data.Entity.EntityState.Deleted;
                db.SaveChanges();
                //方式三
                string role3 = @"delete roles where ;
                db.Database.ExecuteSqlCommand(role3);
                //方式四
                db.cp_delete_role(5);
                Console.Read();
            } 
        }
    }
}
查询的四种方法----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RbacDBEntities db = new RbacDBEntities()) {
                //方式一
                var role = db.Roles.Where(p=>p.Name.Contains("主任")).ToList();
                //方式二方法查询语法
                var role2 = from p in db.Roles
                            where p.Name.Contains("方式")
                            select p;
                //方式三调用sql语句
                string role3 = @"select * from roles where name like'%方%'";
                db.Database.SqlQuery<Role>(role3);
                //方式四
                db.cp_select_role("方法");
                //或List<cp_select_role_Result> roles = db.cp_select_role("方法").ToList();
                Console.Read();
            } 
        }
    }
}

栏目