C# EF
参考网址: https://www.icode9.com/content-1-12930.html
委托到lambda的由来
public delegate int AddSum(int a,int b);
class Program{
static void Main(string[] args)
{
Program p = new Program();
// AddSum addSum = new AddSum(p.Add);
// AddSum addSum = delegate(int a, int b) { return a + b; };
//AddSum addSum = (int a, int b) => { return a + b; };
AddSum addSum = ( a, b) =>a + b;
int sum = addSum(5, 3);
Console.WriteLine(sum);
Console.ReadKey();
}
}
EFFristModelEntities db = new EFFristModelEntities();
db.UserInfo.Add(userInfo);//将数据添加到EF并且添加了添加标记。
db.SaveChanges();//数据才会保存到数据库。,返回受影响的行数。
var userInfoList = from u in db.UserInfo
where u.ID ==343
select u;
foreach (UserInfo userInfo in userInfoList){ //EF 延迟加载机制,数据用到的时候才去数据库中查询。不能用的时候不查询。
Response.Write(userInfo.UserName);
}
Linq语句和select 语句很像
// select * from UserInfo where ID=343
// from UserInfo
// where ID=343
// select * SQL的执行顺序
FirstOrDefault();//返回第一个元素,如果没有的话,返回null
db.UserInfo.Remove(userInfo);
db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Deleted; 这两句作用一样
UserInfo userInfo = new UserInfo() {ID=344};
//db.UserInfo.Remove(userInfo); 通过创建主键存在的对象 不能够操作成功 但是下面打标记的方式可以操作成功
db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Deleted;
默认的已经开启了事务。 工作单元模式。(UnitOfwork)
Model2Container db = new Model2Container();
Customer customer = new Customer() {CustomerName="zhangsan",CustomerPwd="123", SubTime=DateTime.Now };
OrderInfo orderInfo1 = new OrderInfo() { ID = Guid.NewGuid(), OrderNum = "10001", CreateDateTime = DateTime.Now,Customer=customer };
OrderInfo orderInfo2 = new OrderInfo() { ID = Guid.NewGuid(), OrderNum = "10002", CreateDateTime = DateTime.Now, Customer = customer };
db.Customer.Add(customer);
db.OrderInfo.Add(orderInfo1);
db.OrderInfo.Add(orderInfo2);
db.SaveChanges();//默认的已经开启了事务。 工作单元模式。(UnitOfwork)
var customer = (from c in db.Customer
where c.ID == 1
select c).FirstOrDefault();
EFFristModelEntities db = null;
if (HttpContext.Current.Items["db"] == null){
db = new EFFristModelEntities();
HttpContext.Current.Items["db"] = db;
}
else{
db = HttpContext.Current.Items["db"] as EFFristModelEntities;
} 将Context设置为 一次请求一个对象
查询部分字段
var userInfoList = from u in db.UserInfo
where u.ID == 343
select new{UName=u.UserName,UPwd=u.UserPass};
var userInfoList = db.UserInfo.Where<UserInfo>(u => u.ID == 3);
//升序排序
var userInfoList = db.UserInfo.Where<UserInfo>(U => true).OrderBy<UserInfo, int>(u => u.ID);
//降序排序
var userInfoList = db.UserInfo.Where<UserInfo>(U => true).OrderByDescending(u => u.ID);
分页查询
var userInfoList = (from u in db.UserInfo
where u.ID > 0
orderby u.RegTime ascending, u.ID descending
select u).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize);
var userInfoList = db.UserInfo.Where<UserInfo>(U => true).OrderByDescending(u => u.UserPass).ThenByDescending<UserInfo, int>(u => u.ID);
//Skip:表示跳过多少条记录, Take取多少条记录
扩展方法
扩展方法所在的类必须是静态类。
扩展方法必须是静态方法。
第一个参数必须是this关键字后面跟的是给哪个类型扩展的。
CodeFirst
public class ClassInfo
{
[Key]
public int Id { get; set; }
[StringLength(32)]
[Required]
public string ClassName { get; set; }
[Required]
public DateTime CreateTime { get; set; }
public virtual ICollection<StudentInfo> StudentInfo { get; set; }
}
public class CodeFirstDbContext : DbContext
{
public CodeFirstDbContext()
: base("name=connStr")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public DbSet<ClassInfo> ClassInfo { get; set; }
public DbSet<StudentInfo> StudentInfo { get; set; }
}
CodeFirstDbContext db = new CodeFirstDbContext();
db.Database.CreateIfNotExists();
排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
ThenByDescending操作符实现按照次关键字对序列进行降序排列。此操作符的查询语法与方法语法略有不同