【EF學習筆記04】----------EF簡單增刪改查

第一步:建立上下文對象sql

using(var db = new Entities())
{
    //數據操做
}

新增

    UserInfo user = new UserInfo()
    {
        UserName = "zhangsan1",
        UserPass = "123123"
    };
    db.UserInfo.Add(user);

刪除

    user = new UserInfo()
    {
        UserID = 4, //根據主鍵刪除
        UserName = "zhangsan",
        UserPass = "12312311111"
    };
    db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Deleted;

說明:此例中雖然指定了UserName和UserPass屬性值,可是EF執行的時候只會根據主鍵UserID生成查詢條件,因此只須要制定UserID便可。數據庫

刪除 方法2

    user = new UserInfo()
    {
        UserID = 13 //根據主鍵刪除
    };
    //Attach的實體事先不能已經在內存中,不然上下文會追蹤到兩個相同鍵名的實體
    db.UserInfo.Attach(user);//將對象添加到EF管理容器中 ObjectStateManager
    db.UserInfo.Remove(user);//將對象包裝類狀態標識爲刪除

刪除 先查詢-再刪除

    //先加載整個實體 再刪除 執行了2次sql 效率略低 
    using (var db = new Entities())
    {
        var user = (from v in db.UserInfo
                    where v.UserID == 14
                    select v).Single();
        db.UserInfo.Remove(user);
        db.SaveChanges();
    }

修改

    user = new UserInfo()
    {
        UserID = 12,
        UserName = "zhangsan",
        UserPass = "987654"
    };
    db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Modified;

修改單個屬性

    user = new UserInfo()
    {
        UserID = 12,
        UserName = "zhangsan"
    };
    db.UserInfo.Attach(user);
    db.Entry<UserInfo>(user).Property<string>(v => v.UserName).IsModified = true;

說明:將UserID=12的用戶名稱改成 zhangsanspa

修改單個實體 先查詢-再修改

    using (var db = new Entities())
    {
        var user = (from v in db.UserInfo where v.UserName == "zhangsan" select v).Single(); user.UserPass = "123456"; db.SaveChanges(); }

說明:將用戶名爲 zhangsan的用戶 密碼修改成:123456code

查詢全部實體

    var users = db.UserInfo;
    foreach (var v in users)
    {
        ObjectDumper.Write(v);//打印實體
    }

查詢單個實體 主鍵查詢

    using (var db = new Entities())
    {
        var user = db.UserInfo.Find(12);
        ObjectDumper.Write(user);
    }

說明:查詢UserID=12的用戶對象

 

備註:全部增刪改操做都須要調用 db.SaveChanges() 方法,才能更新到數據庫。blog

 

完整案例:內存

 

using(var db = new Entities())
{
    //新增
    UserInfo user = new UserInfo()
    {
        UserName = "zhangsan1",
        UserPass = "123123"
    };
    db.UserInfo.Add(user);
                
    //刪除
    user = new UserInfo()
    {
        UserID = 4, //根據主鍵刪除
        UserName = "zhangsan",
        UserPass = "12312311111"
    };
    db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Deleted;

    //刪除 方法2
    user = new UserInfo()
    {
        UserID = 13 //根據主鍵刪除
    };
    //Attach的實體事先不能已經在內存中,不然上下文會追蹤到兩個相同鍵名的實體
    db.UserInfo.Attach(user);//將對象添加到EF管理容器中 ObjectStateManager
    db.UserInfo.Remove(user);//將對象包裝類狀態標識爲刪除

    //刪除單個實體
    //先加載整個實體 再刪除 執行了2次sql 效率略低 
    user = (from v in db.UserInfo
                where v.UserID == 14
                select v).Single();
    db.UserInfo.Remove(user);
    db.SaveChanges();

    //修改
    user = new UserInfo()
    {
        UserID = 12,
        UserName = "zhangsan",
        UserPass = "987654"
    };
    db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Modified;
                
    //修改單個屬性
    db.UserInfo.Attach(user);
    db.Entry<UserInfo>(user).Property<string>(v => v.UserName).IsModified = true;

    //修改單個實體
    user = (from v in db.UserInfo
                where v.UserName == "zhangsan"
                select v).Single();
    user.UserPass = "abcd";
    db.SaveChanges();

    //查詢遍歷
    var users = db.UserInfo;
    foreach (var v in users)
    {
        ObjectDumper.Write(v);
    }

    //查詢單個實體
    user = db.UserInfo.Find(12);
    ObjectDumper.Write(user);

    //保存更新到數據庫
    db.SaveChanges();
}
相關文章
相關標籤/搜索