MVC EF 增 删 改 查

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//
using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;
using Mvc01.Models;

namespace Mvc01.Common
{
    public class PubMethod
    {
        public void AddTest()
        {
            //0.设置Model1.edms视图中,主键ID为 Identity (同一时刻加入)
            //Entities5 db = new Entities5();

//一、单表
            //1.添加
            //USER1 u1 = new USER1();
            //u1.UNAME1 = "12345";
            //db.USER1.Add(u1);

//2修改 (需要指定主键)

//2.1先查询再更新
            //USER1 u1 = db.USER1.Where(p => p.UID1 == 90).FirstOrDefault();
            //if (u1 != null)
            //{
            //    u1.UNAME1 = "User0000";
            //}

//2.2不用查询 直接更新

MVC EF 增 删 改 查

////3.删除
            //USER1 u1 = new USER1();
            //u1.UID1 = 90;
            //db.Entry(u1).State = EntityState.Deleted;

//4.查询
            //IQueryable<USER1> uAll = db.USER1.Select(p => p);
            //USER1 u2= db.USER1.Where(p => p.UID1 == 90).FirstOrDefault();
            ////贪婪加载
            //STUDENT stu2 = db.STUDENT.Where(p => p.STUID == 42&&p.NUM>5&&p.USER1.Any(b =>b.UDATE>new DateTime(2014,03,01))).FirstOrDefault();
            ////遍历
            //List<STUDENT> stuList = db.STUDENT.Include("STUDENT").ToList();
            //foreach (var item in stuList)
            //{
            //}

//二、多表
            //1.增
            ////1.1多级添加
            //TEACHER te = new TEACHER();
            //te.TNAME = "teacher11";
            //STUDENT stu = new STUDENT();
            //stu.STUNAME = "stu11";
            //USER1 user = new USER1();
            //user.UNAME1 = "user011";

////父子关系
            //te.STUDENT.Add(stu);
            ////子孙关系
            //stu.USER1.Add(user);

////添加操作
            //db.TEACHER.Add(te);

////1.2查找后 增 删 改
            //1.2.1更新子表
            //STUDENT stu = db.STUDENT.Where(p => p.STUID == 42).FirstOrDefault();
            //stu.SNAME2 = "aa";
            //USER1 u1= stu.USER1.Where(p => p.UID1 == 85).FirstOrDefault();
            //if (u1 != null)
            //{
            //    //u1.UNAME1 = "我们1";
            //}

//1.2.2添加子表
            //STUDENT stu = db.STUDENT.Where(p => p.STUID == 42).FirstOrDefault();
            //stu.SNAME2 = "aa";
            //USER1 u2 = new USER1();
            //u2.UNAME1 = "Add01";
            //stu.USER1.Add(u2);

////1.2.3删除子表
            //STUDENT stu = db.STUDENT.Where(p => p.STUID == 42).FirstOrDefault();
            ////遍历主表中的子表,符合条件的子表删除
            //USER1 u1 = new USER1();
            //u1.UID1 = 91;
            //db.Entry(u1).State = EntityState.Deleted;

//级联删除
            //1.设置数据库中Cascade:级联删除
            //STUDENT stu1 = new STUDENT();
            //stu1.STUID = 42;
            //db.Entry(stu1).State = EntityState.Deleted;

//批量添加
            //批量修改
            //批量删除===
            //List<USER1> userList = db.USER1.Where(p => p.SID == 52).ToList();
            //foreach (USER1 item in userList)
            //{
            //    db.Entry(item).State = EntityState.Deleted;
            //}

//子表多对象的操作
            //STUDENT stu = db.STUDENT.Where(p => p.STUID == 43).FirstOrDefault();
            //foreach (var user in stu.USER1)
            //{
            //    if(user.UID1==40)
            //    {
            //        db.Entry(user).State = EntityState.Deleted;
            //    }
            //    if(user.UID1==41)
            //    {
            //        user.UNAME1 = "dddd";
            //    }
            //}
            //USER1 us1 = new USER1();
            //us1.UNAME1 = "eee";
            //stu.USER1.Add(us1);

Entities5 db = new Entities5();

USER1 u1 = new USER1();
            u1.UID1 = 90;  //指定主键
            u1.UNAME1 = "aaaa";
            //设置都不更新
            db.Entry(u1).State = EntityState.Unchanged;
            //单独设置属性
            db.Entry(u1).Property("UNAME1").IsModified = true;

db.SaveChanges();

}

}
}

上一篇:iOS 图片压缩方法


下一篇:GCC 4.8.2 编译安装