EF增删查改加执行存储过程和sql语句,多种方法汇总

ActionUrl c = new ActionUrl() { ActionName="test", RequestUrl="/123/123", SubTime=DateTime.Now };
            //增
            using (EntityContext db = new EntityContext())
            {

                /*方法1*/
                db.ActionUrls.Add(c);
                db.SaveChanges();
                /*方法2*/
                db.Set<ActionUrl>().Attach(c);
                db.Entry<ActionUrl>(c).State = EntityState.Added;
                db.SaveChanges();
                //return c;
                /*方法3*/
                //EF4.0的写法  
                //db.CreateObjectSet<T>().AddObject(entity);

            }
            //查
            using (EntityContext db = new EntityContext())
            {
                c = db.ActionUrls.Where(t => t.ActionName == "test").FirstOrDefault();
            }
            //改
            using (EntityContext db = new EntityContext())
            {
                /*方法1*/
                c.ActionName = "test001";
                db.ActionUrls.Attach(c);
                db.Entry<ActionUrl>(c).State = EntityState.Modified;
                db.SaveChanges();
                /*方法2*/
                c.ActionName = "test002";
                db.SaveChanges();
                //return db.SaveChanges() > 0;
                /*方法3*/
                //EF4.0的写法 
                //db.CreateObjectSet<T>().Addach(entity);
                //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
            }
            //删
            using (EntityContext db = new EntityContext())
            {
                /*方法1*/
                //此处删除的对象不能是自己定义出来的对象,只能是数据库查询出来的对象
                c = db.ActionUrls.Where(t => t.ActionName == "test").FirstOrDefault();
                db.ActionUrls.Remove(c);
                db.SaveChanges();
                /*方法2*/
                //c = db.ActionUrls.Where(t => t.ActionName == "test").FirstOrDefault();
                db.Set<ActionUrl>().Attach(c);
                db.Entry<ActionUrl>(c).State = EntityState.Deleted;
                db.SaveChanges();
                //return db.SaveChanges() > 0;
                /*方法3*/
                //EF4.0的写法 
                //db.CreateObjectSet<T>().Addach(entity);
                //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted);
            }

            //存储过程和sql语句
            EntityContext dbsql = new EntityContext();
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@name",SqlDbType.NVarChar)
            };
            parameters[0].Value = "test";
            
            //执行strSql/procSql
            //返回受影响的行数
            int i = dbsql.Database.ExecuteSqlCommand("exec getActionUrlId @name", parameters);

            List<ActionUrl> dd01 = dbsql.Database.SqlQuery(typeof(ActionUrl), "exec getActionUrlId @name", parameters).Cast<ActionUrl>().ToList();
            //执行strSql/procSql
            //返回数据的集合
            List<ActionUrl> dd02 = dbsql.Database.SqlQuery<ActionUrl>("exec getActionUrlId @name", parameters).ToList();//返回查询到的实体集合

  

posted @ 2016-01-23 10:45  Akgu  阅读(237)  评论(0编辑  收藏  举报