ef增删改查


  1. <pre name="code" class="csharp"//EF查询  
  2.         public userinfo GetUser(string username)  
  3.         {  
  4.             using (MyShopDBEntities1 entity = new MyShopDBEntities1())  
  5.             {  
  6.                userinfo  user = entity.userinfo.SingleOrDefault(model => model.username == username);  
  7.                 return user;  
  8.             }  
  9.         }  
  10.           
  11.         //修改  
  12.         public userinfo Update(string username,string userpwd)  
  13.         {  
  14.               
  15.             using (MyShopDBEntities1 entity = new MyShopDBEntities1())  
  16.             {  
  17.                // userinfo user = entity.userinfo.SingleOrDefault(model => model.username == username);  
  18.                 userinfo user = entity.userinfo.FirstOrDefault(model => model.username == username);  
  19.                 if (user != null)   
  20.                 {   user.userpassword=userpwd;  
  21.                     entity.SaveChanges();   
  22.                 }  
  23.                   
  24.                 return user;   
  25.             }   
  26.              
  27.         }  
  28.   
  29.          
  30.   
  31.         //添加  
  32.         public userinfo Add(userinfo user)  
  33.         {  
  34.             using (MyShopDBEntities1 entity = new MyShopDBEntities1())  
  35.             {  
  36.                 entity.userinfo.Add(  
  37.   
  38.                 new userinfo()  
  39.                 {  
  40.                     username = user.username,  
  41.                     userpwd = user.userpwd  
  42.                 });  
  43.                 if (entity.SaveChanges() > 0)  
  44.                 {  
  45.                     return user;  
  46.                 }  
  47.                 else  
  48.                     return null;  
  49.             }  
  50.         }  
  51.   
  52.         //删除  
  53.         public void Deleted(string  username)  
  54.         {  
  55.             using (MyShopDBEntities1 entity = new MyShopDBEntities1())  
  56.             {  
  57.                 userinfo user = entity.userinfo.FirstOrDefault(m => m.username == username);  
  58.                 if (user != null)  
  59.                 {  
  60.                    entity.userinfo.Remove(user);  
  61.                 }  
  62.   
  63.                 if (entity.SaveChanges() > 0)  
  64.                 {  
  65.                     //return 0;  
  66.                 }  
  67.   
  68.             }  
  69.         }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EF(Entity Framework)是.NET Framework中的一种ORM(Object Relational Mapping)框架,可以用来简化数据库操作。下面是使用EF Web API进行增删改查的示例代码: 1. 增加数据: ```csharp [HttpPost] public async Task<IActionResult> AddData([FromBody] Data data) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _context.Data.Add(data); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(GetData), new { id = data.Id }, data); } ``` 2. 删除数据: ```csharp [HttpDelete("{id}")] public async Task<IActionResult> DeleteData(int id) { var data = await _context.Data.FindAsync(id); if (data == null) { return NotFound(); } _context.Data.Remove(data); await _context.SaveChangesAsync(); return NoContent(); } ``` 3. 修改数据: ```csharp [HttpPut("{id}")] public async Task<IActionResult> UpdateData(int id, [FromBody] Data data) { if (id != data.Id) { return BadRequest(); } _context.Entry(data).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_context.Data.Any(e => e.Id == id)) { return NotFound(); } else { throw; } } return NoContent(); } ``` 4. 查询数据: ```csharp [HttpGet("{id}")] public async Task<ActionResult<Data>> GetData(int id) { var data = await _context.Data.FindAsync(id); if (data == null) { return NotFound(); } return data; } ``` 以上是EF Web API进行增删改查的示例代码,需要注意的是,这只是示例代码,具体实现还需要根据具体的业务逻辑进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值