一丶执行
class Program { static void Main(string[] args) { var db = new TestDBEntities(); string sql = @"select * from UserInfor where id =@id"; SqlParameter[] parameter = { new SqlParameter("@id","0011"), }; var userLit = db.Database.SqlQuery<UserInfor>(sql, parameter).ToList(); foreach (var item in userLit) { Console.WriteLine("userName:" + item.userName); } } }
二丶增删改使用(
ExecuteSqlCommand
)
三、批量删除和修改
使用插件:EntityFramework.Extended.6.1.0.168
引用命名空间:using EntityFramework.Extensions;
db.Set<Sys_RoleAndAuthority>().Where(u => u.RoleId == new Guid(id)).Delete(); //删除 db.Set<Sys_MenuAuthority>().Where(c=>c.Id==id).Update(p => new Sys_MenuAuthority { AuthorityName =AuthorityName, OrderValue = orderBy }); //修改
四、EF 使用事务
[HttpPost] public JsonResult Del(string id) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { //删除角色 db.Set<RoleInfo>().Where(u => u.id == new Guid(id)).Delete(); //删除角色对应的菜单权限 db.Set<Sys_RoleAndAuthority>().Where(u => u.RoleId == new Guid(id)).Delete(); //删除角色对应的数据权限 db.Set<Sys_RoleAndDataAuthority>().Where(u => u.roleId == new Guid(id)).Delete(); transaction.Commit(); return Json(new { status = "success" }); } catch (Exception ex) { transaction.Rollback(); // 事物回滚 WriteLog.WirteErrLog(ex); //记录错误日志 return Json(new { status = "error" }); } } }