Add
/// <summary>
///
/// </summary>
public void Add()
{
TestDBEntities2 testdb = new TestDBEntities2();
Test1 test1 = new Test1() { Name="yang"};
testdb.Tests.Add(test1);
testdb.SaveChanges();
}
Modify
public void Modify()
{
//-------方法1--------
// //1.先查询出来
Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
////2修改内容
test1.Name = "yang123";
////3 提交
testdb.SaveChanges();
//4. 提交
testdb.SaveChanges();
}
public void Modify2()
{
//1.new对象
Test1 test1 = new Test1() { Id = , Name = "yang1" };
#region 修改部分字段
//2.附加对象
testdb.Tests.Attach(test1);
//3.修改部分字段
testdb.Entry(test1).Property("Name").IsModified = true;
#endregion
#region 修改全部字段
//2.修改全部字段
testdb.Entry(test1).State = System.Data.EntityState.Modified;
#endregion
//4. 提交
testdb.SaveChanges();
}
Delete
public void Delete()
{
//1.先查
Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
//2.再删除
testdb.Tests.Remove(test1);
//3.提交
testdb.SaveChanges();
}
public void Delete2()
{
//1. new对象
Test1 test1 = new Test1() { Name = "yang" };
//2.附加对象
testdb.Tests.Attach(test1);
//3.删除对象
testdb.Tests.Remove(test1);
//4. 提交
testdb.SaveChanges();
}
Select
public void Select()
{
List<Test1> list;
List<Test1> list1;
//开始没有查询 ToList后才开始查询
list = testdb.Tests.Where(u => u.Name == "yang").ToList();
list1 = (from u in testdb.Tests where u.Name == "yang" select u).ToList();
}