EF增删查改

一、增加

using (var entities = new TrainingDemoEntities())

{

    ar addLogin = new tb_login();

    addLogin.UserName = "entity";

    addLogin.UserPwd = "entityPwd";

    entities.tb_login.AddObject(addLogin);

    entities.SaveChanges();

}

二、删除

using (var entities = new TrainingDemoEntities())

{

    int id = 8;

    var data = entities.tb_login.FirstOrDefault(p => p.Id == id);

    if (data != null)

    {

         entities.tb_login.DeleteObject(data);

         entities.SaveChanges();

    }    

}

三、查询

using (var entities = new TrainingDemoEntities())

{

    var data = entities.tb_login.ToList();

    foreach (var i in data)

    {

        var tb = new TextBlock { Text = i.UserName };

        spRoot.Children.Add(tb);

    }

}

四、修改

using (var entities = new TrainingDemoEntities())

{

    int id = 7;

    var data = entities.tb_login.FirstOrDefault(p => p.Id == id);

    if (data != null)

    {

        data.UserName = "update";

        data.UserPwd = "updatePwd";

        entities.SaveChanges();

    }

}

 

你可能感兴趣的:(EF增删查改)