using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myAppLearnEntity
{
class Program
{
static void Main(string[] args)
{
//(1)添加实体对象数据到数据库
//TestEntities test = new TestEntities();
//Book book = new Book();
//book.Name = "test111";
//test.Book.AddObject(book);
//test.SaveChanges();
//(2)修改实体对象数据同步到数据库
/*
TestEntities test = new TestEntities();
Book book = new Book();
book.ID = 1;
book.Name = "test100";
test.Book.Attach(book);
test.ObjectStateManager.ChangeObjectState(book, System.Data.EntityState.Modified);
test.SaveChanges();
*/
//(3)删除实体对象数据同步到数据库
/*
TestEntities test = new TestEntities();
Book book = new Book();
book.ID = 3;
//book.Name = "test100";
test.Book.Attach(book);
test.ObjectStateManager.ChangeObjectState(book, System.Data.EntityState.Deleted);
test.SaveChanges();
*/
//(4)查询数据(查询多条语句)
//TestEntities test = new TestEntities();
//var datas = from c in test.Book
// where c.ID < 5
// select c;
//foreach (var item in datas)
//{
// Console.WriteLine(item.Name);
//}
//(5)查询数据(查询单条语句)
TestEntities test = new TestEntities();
//写法一
//var datas = (from c in test.Book
// where c.ID == 5
// select c).SingleOrDefault();
//写法二
//var datas = (from c in test.Book
// where c.ID == 2
// select c).FirstOrDefault();
//if (datas!=null)
//{
// Console.WriteLine(datas.Name);
//}
//(6)级联查询
TestEntities test = new TestEntities();
var list = ( from b in test.Book
join s in test.Student
on b.StudentID equals s.ID into userrooms
from ur in userrooms.DefaultIfEmpty()
select new
{
id=ur.ID,
name=ur.Name
}).ToList();
foreach (var item in list)
{
Console.WriteLine(item.name);
}
Console.ReadKey();
}
}
}