1. 程式人生 > >Ef執行Sql查詢

Ef執行Sql查詢

[] execute SQ 當前 sce linq exec index 過程

1、無參數查詢 var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList(); 2、有參查詢 var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes where id=@ID ",new SqlParameter("@ID",id)).ToList(); 3、結合linq查詢 var model = (from p in db.userinfo.SqlQuery("select * from UserInfoes where id=@ID"
, new SqlParameter("@ID", 1)) select p).ToList(); 4:EF原始查詢單獨表 model = db.userinfo.OrderByDescending(a => a.ID).Skip(10 * (當前頁 - 1)).Take(10).ToList(); 5,EF 執行存儲過程或delete 刪除語句 SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ID",id) }; db.Database.ExecuteSqlCommand("delete UserInfoes where id=@ID"
, para); 存儲過程 刪除,有幾個參數,存儲後面要帶幾個參數以逗號分隔 SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ID",id) }; db.Database.ExecuteSqlCommand("sp_Userinfos_deleteByID @ID", para);

Ef執行Sql查詢