EntityFramwork 七七八八

Tip
    技术的选型受技术先进性、技术持续性、技术普及度、推行力度的影响。
    我也很无奈,一大把年纪了又要重新学一种ORMapping框架。
说实话,这是我用过的最复杂的ORMapping框架了。 EntityFramework 微软推出的ORMapping方案,可用代码来描述(CodeFirst) 实体类和数据库是对应的,可自动生成数据库表 实体类和数据操作是分开的。必须另外写一套代码操作数据。实体类可归为Model层,操作类可归为DAL(或BLL)层 EF6.x 目前只能通过 NuGet 进行管理,所以,首先确定你的 Visual Studio 中的 NuGet 为最新版本(最低支持
2.8.3,最新版本 3.0)。 本文以最新语法为准,与老版本区别之处可参考:http://blog.csdn.net/chenyq2008/article/details/41659205 又出来一个 EntityFramwork core(原先叫 EF 7,基于.net core),区别见 http://www.cnblogs.com/n-pei/p/4274907.html http://www.cnblogs.com/xishuai/p/ef7-develop-note.html https://docs.microsoft.com/en-us/ef/efcore-and-ef6/index 架构 EDM (实体数据模型):EDM包括三个模型,概念模型、 映射和存储模型。 概念模型 ︰ 概念模型包含模型类和它们之间的关系。独立于数据库表的设计。 存储模型 ︰ 存储模型是数据库设计模型,包括表、 视图、 存储的过程和他们的关系和键。 映射 ︰ 映射包含有关如何将概念模型映射到存储模型的信息。 LINQ to Entities ︰ LINQ to Entities 是一种用于编写针对对象模型的查询的查询语言。它返回在概念模型中定义的实体。 Entity SQL: Entity SQL 是另一种炉类似于L2E的言语,但相给L2E要复杂的多,所以开发人员不得不单独学习它。 Object Services(对象服务):是数据库的访问入口,负责数据具体化,从客户端实体数据到数据库记录以及从数据库记录和实体数据的转换。 Entity Client Data Provider:主要职责是将L2E或Entity Sql转换成数据库可以识别的Sql查询语句,它使用Ado.net通信向数据库发送数据可获取数据。 ADO.Net Data Provider:使用标准的Ado.net与数据库通信 三种模式 CodeFirst 先定义实体类,用实体类生成数据库 Database First 从数据库生成实体类 Model First 使用Visual Studio实体设计器,设计ER,同时生成Entity类和DB 参考 http://www.cnblogs.com/libingql/p/3349866.html https://github.com/aspnet/EntityFramework/wiki https://docs.microsoft.com/en-us/ef/ http://www.cnblogs.com/xuf22/articles/5513283.html http://www.entityframeworktutorial.net/code-first-with-entity-framework.aspx http://www.cnblogs.com/guomingfeng/category/480047.html 安装 install-package entityframework ---------------------------------------------- 数据库上下文(DbContext) ---------------------------------------------- DbContext主要负责以下活动: EntitySet: Dbctx包含了所有映射到表的entities Querying:将Linq-To-Entities转译为Sql并发送到数据库 Change Tracking:从数据库获取entities后保留并跟踪实体数据变化 Persisting Data:根据entity状态执行Insert、update、delete命令 Caching:Dbctx的默认第一级缓存,在上下文中的生命周期中存储entity Manage Relationship:Dbctx在DbFirst模式中使用CSDL、MSL、SSDL管理对象关系,Code first中使用fluent api 管理关系 Object Materialization:Dbctx将物理表转成entity实例对象 示例 using System.Data.Entity; namespace App { public class AppContext : DbContext { // 构造函数,此处指定了数据库连接字符串 public AppContext() : base("Default") {} // 实体集合 public DbSet Depts { get; set; } public DbSet Users { get; set; } public DbSet Roles { get; set; } // 创建数据库 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); Database.SetInitializer(new MigrateDatabaseToLatestVersion()); } } } using (var ctx = newSchoolDBEntities()) { //Can perform CRUD operation using ctx here.. } ---------------------------------------------- 实体类(Entity) ---------------------------------------------- POCO类(不依赖于任何框架的实体类) using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace App { public class User : IKeyID { // 主键 [Key] public int ID { get; set; } // 必填;长度约束 [Required, StringLength(50)] public string Name { get; set; } // 可空属性 public DateTime? Birthday { get; set; } // 关联对象 public virtual ICollection Roles { get; set; } public virtual ICollection Titles { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> Dept Dept { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } } } 外键约束(父子表关系)RelationShip </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> ICollection<Role> Roles { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span>----------------------------------------------<span style="color:#000000;"> DbSet </span>----------------------------------------------<span style="color:#000000;"> usage </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> DbSet<User> Users{<span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">;} method Add(entity) : 添加 AsNoTracking</span><entity><span style="color:#000000;"> : 不缓存,可增加性能 Attach(entity) : 附加实体 Find(</span><span style="color:#0000ff;">int</span><span style="color:#000000;">) : 查找(根据主键) Create : 创建(注意创建后还需Add操作) Include : 加载附加子对象 Remove : 删除实体 SqlQuery : 执行Sql语句 </span>----------------------------------------------<span style="color:#000000;"> 标注 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.cnblogs.com/libingql/p/3352058.html</span> ----------------------------------------------<span style="color:#000000;"> Entity Framework Code First 约定 表名为:类名的英语复数形式,创建的表所有者为dbo 主键为:ID 或 类名 </span>+<span style="color:#000000;"> ID 对于int类型的主键,会自动的设置其为自动增长列 用Annotation标注 可使用System.ComponentModel.DataAnnotations来指定表名、字段名、字段约束等信息 [Table(</span><span style="color:#800000;">"</span><span style="color:#800000;">Product</span><span style="color:#800000;">"</span>, Schema = <span style="color:#800000;">"</span><span style="color:#800000;">dbo</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 表名、所有者</span> [Key] <span style="color:#008000;">//</span><span style="color:#008000;"> 主键字段</span> [Column(<span style="color:#800000;">"</span><span style="color:#800000;">MenuID</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 字段名</span> [Required] <span style="color:#008000;">//</span><span style="color:#008000;"> 字段必填</span> [TypeName = <span style="color:#800000;">"</span><span style="color:#800000;">MONEY</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 字段类型</span> [StringLength(<span style="color:#800080;">50</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 字段长度</span> [DatabaseGenerated(DatabaseGeneratedOption.None)] <span style="color:#008000;">//</span><span style="color:#008000;"> 主键不自动增长</span> [DatabaseGenerated(DatabaseGeneratedOption.Identity)] <span style="color:#008000;">//</span><span style="color:#008000;"> 主键自增长</span> [DatabaseGenerated(DatabaseGeneratedOption.Computed)] <span style="color:#008000;">//</span><span style="color:#008000;"> 该列是通过其它列计算得出的,不会将其持久化到数据库中。只读?类似视图。</span> [NotMapped] <span style="color:#008000;">//</span><span style="color:#008000;"> 属性不匹配数据库字段</span> [TimeStamp] <span style="color:#008000;">//</span><span style="color:#008000;"> 时间戳。详见《并发处理》章节</span> [ConcurrencyCheck ] <span style="color:#008000;">//</span><span style="color:#008000;"> 并发检测。详见《并发处理》章节</span> [ForeignKey(<span style="color:#800000;">"</span><span style="color:#800000;">CatID</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 外键名称。</span> [Index(<span style="color:#800000;">"</span><span style="color:#800000;">TitleIndex</span><span style="color:#800000;">"</span>, IsUnique = <span style="color:#0000ff;">true</span>)] <span style="color:#008000;">//</span><span style="color:#008000;"> 唯一键</span> <span style="color:#000000;"> eg [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] </span><span style="color:#0000ff;">public</span> Guid SocialSecurityNumber { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } 用代码方式指定(FluentAPI) </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> XXXContext : DbContext { ... </span><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> OnModelCreating(DbModelBuilder modelBuilder) { </span><span style="color:#008000;">//</span><span style="color:#008000;"> 设置 Blog 表</span> <span style="color:#0000ff;">var</span> entity = modelBuilder.Entity<Blog><span style="color:#000000;">(); entity .ToTable(</span><span style="color:#800000;">"</span><span style="color:#800000;">MyBlog</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">dbo</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 表名(EF6)</span> .HasOptional(x => x.ParentMessage) <span style="color:#008000;">//</span><span style="color:#008000;"> 表名(EF7)</span> .HasKey(x => x.BlogId); <span style="color:#008000;">//</span><span style="color:#008000;"> 主键</span> .HasKey(t => <span style="color:#0000ff;">new</span> { t.KeyID, t.CandidateID }) <span style="color:#008000;">//</span><span style="color:#008000;"> 复合主键</span> .HasMany(r => r.Users) <span style="color:#008000;">//</span><span style="color:#008000;"> 1对多关系</span> .WithMany(u => u.Roles) <span style="color:#008000;">//</span> .Map(x => x.ToTable(<span style="color:#800000;">"</span><span style="color:#800000;">RoleUsers</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 关联表</span> .MapLeftKey(<span style="color:#800000;">"</span><span style="color:#800000;">RoleID</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 左键关联</span> .MapRightKey(<span style="color:#800000;">"</span><span style="color:#800000;">UserID</span><span style="color:#800000;">"</span>)) <span style="color:#008000;">//</span><span style="color:#008000;"> 右键关联</span> .Ignore(t => t.Remark) <span style="color:#008000;">//</span><span style="color:#008000;"> 忽略属性 </span> .HasRequired(t => t.Category) <span style="color:#008000;">//</span><span style="color:#008000;"> Category的值来自外键</span> .WithMany(t => t.Products) <span style="color:#008000;">//</span> .HasForeignKey(d => d.CatID) <span style="color:#008000;">//</span> <span style="color:#000000;"> } ; </span><span style="color:#008000;">//</span><span style="color:#008000;"> 设置 Blog.Title 字段</span> <span style="color:#000000;"> entity .Property(x </span>=> x.Title) <span style="color:#008000;">//</span><span style="color:#008000;"> 字段</span> .IsRequired() <span style="color:#008000;">//</span><span style="color:#008000;"> 必填</span> .HasMaxLength(<span style="color:#800080;">50</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 最大长度</span> .HasColumnName(<span style="color:#800000;">"</span><span style="color:#800000;">TitleColumn</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;"> 字段名</span> .HasColumnType(<span style="color:#800000;">"</span><span style="color:#800000;">text</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 字段类型</span> .HasPrecision(<span style="color:#800080;">18</span>, <span style="color:#800080;">2</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 数字类型的精度设置</span> .GenerateValuesOnAdd(<span style="color:#0000ff;">false</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 不自增</span> .ManyToOne(n => n.Entity, t => t.ChildEntities)<span style="color:#008000;">//</span><span style="color:#008000;"> 多对1</span> .ForeignKey(t => t.EntityId) <span style="color:#008000;">//</span><span style="color:#008000;"> 外键</span> <span style="color:#000000;"> ; </span><span style="color:#008000;">//</span><span style="color:#008000;"> 可统一设置所有库表前缀</span> modelBuilder.Types().Configure(f => f.ToTable(<span style="color:#800000;">"</span><span style="color:#800000;">TablePrefix_</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> f.ClrType.Name)); } } 或更复杂一点,将配置独立建一个类 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">download.csdn.net/detail/zhanxueguang/8950589</span> <span style="color:#000000;"> 新建表配置类 </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Data.Entity.ModelConfiguration; </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> BlogConfig : EntityTypeConfiguration<Blog><span style="color:#000000;"> { </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> BlogConfig() { </span><span style="color:#0000ff;">this</span>.ToTable(<span style="color:#800000;">"</span><span style="color:#800000;">Blog</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;">表名</span> <span style="color:#0000ff;">this</span>.HasKey(x => x.BlogId); <span style="color:#008000;">//</span><span style="color:#008000;">设置主键</span> <span style="color:#0000ff;">this</span>.Property(x => x.Title).IsRequired().HasMaxLength(<span style="color:#800080;">50</span>); <span style="color:#008000;">//</span><span style="color:#008000;">设置字段属性,约束 not null, 最大长度50</span> <span style="color:#0000ff;">this</span>.Property(x => x.Content).HasColumnType(<span style="color:#800000;">"</span><span style="color:#800000;">text</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;">设置字段类型为 text</span> <span style="color:#0000ff;">this</span>.Property(x => x.CreateTime).IsRequired(); <span style="color:#008000;">//</span><span style="color:#008000;"> 设置字段约束 not null</span> <span style="color:#0000ff;">this</span>.HasMany(x => x.Comments); <span style="color:#008000;">//</span><span style="color:#008000;"> 设置导航字段, Blog: Comment --> one: many</span> <span style="color:#000000;"> } } 注册配置类 </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Linq; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Data.Entity; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Reflection; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Data.Entity.ModelConfiguration; </span><span style="color:#0000ff;">namespace</span><span style="color:#000000;"> CodeFirstSample { </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> CodeFirstContext : DbContext { </span><span style="color:#0000ff;">public</span> CodeFirstContext() : <span style="color:#0000ff;">base</span>(<span style="color:#800000;">"</span><span style="color:#800000;">CodeFirstContext</span><span style="color:#800000;">"</span><span style="color:#000000;">){} </span><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(</span><span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogConfig()); </span><span style="color:#0000ff;">base</span><span style="color:#000000;">.OnModelCreating(modelBuilder); } </span><span style="color:#008000;">//</span><span style="color:#008000;"> 获取相关模型的数据集,使用 new 关键字来隐藏父类的 Set<> 方法</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">new</span> IDbSet<TEntity> Set<TEntity>() <span style="color:#0000ff;">where</span> TEntity : <span style="color:#0000ff;">class</span><span style="color:#000000;"> { </span><span style="color:#0000ff;">return</span> <span style="color:#0000ff;">base</span>.Set<TEntity><span style="color:#000000;">(); } } } </span>----------------------------------------------<span style="color:#000000;"> 操作数据(CRUD) 三种查询方式 </span><span style="color:#800080;">1</span><span style="color:#000000;">) LINQ to Entities </span><span style="color:#800080;">2</span><span style="color:#000000;">) Entity SQL </span><span style="color:#800080;">3</span><span style="color:#000000;">) Native SQL </span>----------------------------------------------<span style="color:#000000;"> Query LINQ to Entities: </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx =<span style="color:#000000;"> newSchoolDBEntities()) { </span><span style="color:#0000ff;">var</span> query = ctx.Students.<span style="color:#0000ff;">where</span>(s => s.StudentName == <span style="color:#800000;">"</span><span style="color:#800000;">Bill</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">var</span> student = query.FirstOrDefault<Student><span style="color:#000000;">(); } </span><span style="color:#0000ff;">var</span> query = ctx.Customers.Where(r => r.Address.City ==<span style="color:#000000;"> city); </span><span style="color:#0000ff;">var</span> query = ctx.Customers.Where(<span style="color:#800000;">"</span><span style="color:#800000;">r.Address.City = '</span><span style="color:#800000;">"</span>+city+<span style="color:#800000;">"</span><span style="color:#800000;">'</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;"> 现在不能用了</span> <span style="color:#0000ff;">var</span> query = ctx.Customer.Where(r => r.CustomerID.Contains(<span style="color:#800000;">"</span><span style="color:#800000;">V</span><span style="color:#800000;">"</span><span style="color:#000000;">)); </span><span style="color:#0000ff;">var</span> query = ctx.Customer.Where(<span style="color:#800000;">"</span><span style="color:#800000;">it.CustomerID LIKE @CustomerID</span><span style="color:#800000;">"</span>, <span style="color:#0000ff;">new</span> ObjectParameter(<span style="color:#800000;">"</span><span style="color:#800000;">CustomerID</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">%V%</span><span style="color:#800000;">"</span><span style="color:#000000;">)); LINQ Query syntax: </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { </span><span style="color:#0000ff;">var</span> query = <span style="color:#0000ff;">from</span> st <span style="color:#0000ff;">in</span> ctx.Students <span style="color:#0000ff;">where</span> st.StudentName == <span style="color:#800000;">"</span><span style="color:#800000;">Bill</span><span style="color:#800000;">"</span><span style="color:#0000ff;">select</span><span style="color:#000000;"> st; </span><span style="color:#0000ff;">var</span> student = query.FirstOrDefault<Student><span style="color:#000000;">(); } </span><span style="color:#0000ff;">var</span> query = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> ctx.Customers <span style="color:#0000ff;">where</span> c.Address.City == city <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; Entity SQL: </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { </span><span style="color:#0000ff;">string</span> sql = <span style="color:#800000;">"</span><span style="color:#800000;">SELECT VALUE st FROM SchoolDBEntities.Students AS st WHERE st.StudentName == 'Bill'</span><span style="color:#800000;">"</span><span style="color:#000000;">; </span><span style="color:#0000ff;">var</span> query = db.CreateQuery<Orders>(<span style="color:#800000;">"</span><span style="color:#800000;">SELECT VALUE Orders FROM Orders WHERE Orders.OrderDate < @dt;</span><span style="color:#800000;">"</span><span style="color:#000000;">, ps); } </span><span style="color:#0000ff;">var</span> query = ctx.CreateQuery<Customers>(<span style="color:#800000;">"</span><span style="color:#800000;">SELECT VALUE c FROM Customers AS c WHERE c.Address.City = @city</span><span style="color:#800000;">"</span>, <span style="color:#0000ff;">new</span> ObjectParameter(<span style="color:#800000;">"</span><span style="color:#800000;">city</span><span style="color:#800000;">"</span><span style="color:#000000;">, city)); Entity SQL </span>&<span style="color:#000000;"> EntityClient </span><span style="color:#0000ff;">var</span> objctx = (ctx <span style="color:#0000ff;">as</span><span style="color:#000000;"> IObjectctxAdapter).Objectctx; </span><span style="color:#0000ff;">var</span> student = objctx.CreateQuery<Student><span style="color:#000000;">(sqlString); </span><span style="color:#0000ff;">var</span> newStudent = student.First<Student><span style="color:#000000;">(); </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> conn = <span style="color:#0000ff;">new</span> EntityConnection(<span style="color:#800000;">"</span><span style="color:#800000;">name=SchoolDBEntities</span><span style="color:#800000;">"</span><span style="color:#000000;">)) { conn.Open(); EntityCommand cmd </span>=<span style="color:#000000;"> con.CreateCommand(); cmd.CommandText </span>= <span style="color:#800000;">"</span><span style="color:#800000;">SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentName='Bill'</span><span style="color:#800000;">"</span><span style="color:#000000;">; </span><span style="color:#0000ff;">var</span> dict = <span style="color:#0000ff;">new</span> Dictionary<<span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">string</span>><span style="color:#000000;">(); </span><span style="color:#0000ff;">using</span> (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess |<span style="color:#000000;"> CommandBehavior.CloseConnection)) { </span><span style="color:#0000ff;">while</span><span style="color:#000000;"> (rdr.Read()) { </span><span style="color:#0000ff;">int</span> a = rdr.GetInt32(<span style="color:#800080;">0</span><span style="color:#000000;">); </span><span style="color:#0000ff;">var</span> b = rdr.GetString(<span style="color:#800080;">1</span><span style="color:#000000;">); dict.Add(a, b); } } } Native SQL(数据库平台相关,不推荐使用) </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { </span><span style="color:#0000ff;">var</span> studentName = ctx.Students.SqlQuery(<span style="color:#800000;">"</span><span style="color:#800000;">Select studentid, studentname, standardId from Student where studentname='Bill'</span><span style="color:#800000;">"</span>).FirstOrDefault<Student><span style="color:#000000;">(); } 评价 解析步骤(都是解析为CommandTree,再解析为数据库依赖的sql语句,本质上并无区别) Linq to Entity </span>---> Command Tree --> SQL --> ADO.net DataProvider --><span style="color:#000000;"> DB Entity SQL 都是数据库平台无关的 Entity SQL 转换的SQL性能会好一点 Linq to Entity 强类型特性,编译时检查 建议临时查询选择EntityCLient,如果是操作数据那只能采用ObjectService CRUD CREATE ctx.Users.Add(</span><span style="color:#0000ff;">new</span> User() { UserName = <span style="color:#800000;">"</span><span style="color:#800000;">New Student</span><span style="color:#800000;">"</span><span style="color:#000000;"> }); ctx.SaveChanges(); READ </span><span style="color:#0000ff;">var</span> users = ctx.Users.ToList<User><span style="color:#000000;">(); </span><span style="color:#0000ff;">var</span> user = ctx.Person.Single(a = >a.Id == <span style="color:#800080;">1</span><span style="color:#000000;">); ctx.Users.Where(...).ToList(); ctx.Users.Find(...).FirstOrDefault() 字符串比较 </span><span style="color:#008000;">//</span><span style="color:#008000;"> where year >= yearFrom and year <= yearTo</span> <span style="color:#0000ff;">string</span> yearFrom = <span style="color:#0000ff;">this</span><span style="color:#000000;">.txtYearFrom.Text; </span><span style="color:#0000ff;">string</span> yearTo = <span style="color:#0000ff;">this</span><span style="color:#000000;">.txtYearTo.Text; </span><span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(yearFrom) && !<span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrEmpty(yearTo)) q </span>= q.Where(t => t.Year.CompareTo(yearFrom)>=<span style="color:#800080;">0</span> && t.Year.CompareTo(yearTo)<=<span style="color:#800080;">0</span><span style="color:#000000;">); </span><span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(yearFrom) && <span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrEmpty(yearTo)) q </span>= q.Where(t => t.Year.CompareTo(yearFrom) >= <span style="color:#800080;">0</span><span style="color:#000000;">); </span><span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">string</span>.IsNullOrEmpty(yearFrom) && !<span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrEmpty(yearTo)) q </span>= q.Where(t => t.Year.CompareTo(yearTo) <= <span style="color:#800080;">0</span><span style="color:#000000;">); UPDATE 方法一:先找到》更改属性》SaveChanges </span><span style="color:#0000ff;">public</span> UpdateUserName(<span style="color:#0000ff;">int</span> id, <span style="color:#0000ff;">string</span><span style="color:#000000;"> userName) { </span><span style="color:#0000ff;">var</span> user = ctx.Users.Where(s => s.ID == id).FirstOrDefault<Student><span style="color:#000000;">(); user.UserName </span>=<span style="color:#000000;"> userName; ctx.SaveChanges(); } 方法二:先设置键值》Attach》更改属性》SaveChanges(相当于直接写update语句) </span><span style="color:#0000ff;">public</span> UpdateUserName(<span style="color:#0000ff;">int</span> id, <span style="color:#0000ff;">string</span><span style="color:#000000;"> userName) { User user </span>= <span style="color:#0000ff;">new</span> User{ID =<span style="color:#000000;"> id}; </span><span style="color:#0000ff;">var</span> entity =<span style="color:#000000;"> ctx.Users.Attach(user); entity.UserName </span>=<span style="color:#000000;"> userName; ctx.SaveChanges(); } 方法三:手动修改EntityState </span><span style="color:#0000ff;">public</span> UpdateUserName(<span style="color:#0000ff;">int</span> id, <span style="color:#0000ff;">string</span><span style="color:#000000;"> userName) { User user </span>= <span style="color:#0000ff;">new</span> User{ID = id, UserName=<span style="color:#000000;">userName}; </span><span style="color:#0000ff;">var</span> entity =<span style="color:#000000;"> ctx.Entity(user); entity.State </span>=<span style="color:#000000;"> EntityState.Modified; entity.Property(</span><span style="color:#800000;">"</span><span style="color:#800000;">UserName</span><span style="color:#800000;">"</span>).IsModified = <span style="color:#0000ff;">true</span><span style="color:#000000;">; ctx.SaveChanges(); } DELETE 方法1:先找到》再删除》SaveChanges </span><span style="color:#0000ff;">var</span> user = ctx.Users.FirstOrDefault(m => m.ID =<span style="color:#000000;"> id); ctx.Users.Remove(user); ctx.SaveChanges(); 方法2:自己创建对象》附加》删除》SaveChanges </span><span style="color:#0000ff;">var</span> user = <span style="color:#0000ff;">new</span> User{ID =<span style="color:#000000;"> id}; ctx.User.Attach(user); ctx.Remove(user); ctx.SaveChanges(); 方法3:自己创建对象》放入EF容器》修改状态》SaveChanges </span><span style="color:#0000ff;">var</span> user = <span style="color:#0000ff;">new</span> User{ID =<span style="color:#000000;"> id}; </span><span style="color:#0000ff;">var</span> entry =<span style="color:#000000;"> ctx.Entry(user); entry.State </span>=<span style="color:#000000;"> EntityState.Deleted; ctx.SaveChanges(); 注:在创建对象的时候,需要指定主键列才行 方法4: 使用扩展 https:</span><span style="color:#008000;">//</span><span style="color:#008000;">github.com/loresoft/EntityFramework.Extended</span> ctx.Users.Delete(u => u.Id =<span style="color:#000000;"> id); 关联表 新增 </span><span style="color:#0000ff;">int</span>[] roleIDs =<span style="color:#000000;"> StringUtil.GetIntArrayFromString(hfSelectedRole.Text); item.Roles </span>= <span style="color:#0000ff;">new</span> List<Role><span style="color:#000000;">(); </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">int</span> roleID <span style="color:#0000ff;">in</span><span style="color:#000000;"> roleIDs) { Role role </span>= <span style="color:#0000ff;">new</span> Role { ID =<span style="color:#000000;"> roleID }; DB.Roles.Attach(role); item.Roles.Add(role); } 删除 清除用户表中的部门字段数据 DB.Users.Include(u </span>=><span style="color:#000000;"> u.Dept) .Where(u </span>=><span style="color:#000000;"> userIDs.Contains(u.ID)) .ToList() .ForEach(u </span>=> u.Dept = <span style="color:#0000ff;">null</span><span style="color:#000000;">) ; 或者反过来(删除部门表的User列表信息) Dept dept </span>= DB.Depts.Include(d =><span style="color:#000000;"> d.Users) .Where(d </span>=> d.ID ==<span style="color:#000000;"> deptID) .FirstOrDefault(); </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">int</span> userID <span style="color:#0000ff;">in</span><span style="color:#000000;"> userIDs) { User user </span>= dept.Users.Where(u => u.ID ==<span style="color:#000000;"> userID).FirstOrDefault(); </span><span style="color:#0000ff;">if</span> (user != <span style="color:#0000ff;">null</span><span style="color:#000000;">) dept.Users.Remove(user); } 更新 先删光,再添加 </span><span style="color:#0000ff;">int</span>[] roleIDs =<span style="color:#000000;"> StringUtil.GetIntArrayFromString(hfSelectedRole.Text); </span><span style="color:#0000ff;">int</span>[] addRoleIDs = roleIDs.Except(item.Roles.Select(r =><span style="color:#000000;"> r.RoleID)).ToArray(); </span><span style="color:#0000ff;">int</span>[] removeRoleIDs = item.Roles.Select(r =><span style="color:#000000;"> r.RoleID).Except(roleIDs).ToArray(); </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">int</span> roleID <span style="color:#0000ff;">in</span><span style="color:#000000;"> addRoleIDs) { </span><span style="color:#0000ff;">var</span> role = <span style="color:#0000ff;">new</span> Role { RoleID =<span style="color:#000000;"> roleID }; DB.Roles.Attach(role); item.Roles.Add(role); } </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">int</span> roleID <span style="color:#0000ff;">in</span><span style="color:#000000;"> removeRoleIDs) { item.Roles.Remove(item.Roles.Single(r </span>=> r.RoleID ==<span style="color:#000000;"> roleID)); } 脱机实体以及状态 Attach Attach用来将某个已知存在于数据库中的实体重新加载到上下文中。SaveChanges不会尝试将Attached的实体插入到数据库中,因为这个实体假设已经存在于数据库中。 User admin </span>= DB.Users.Where(u => u.Name == <span style="color:#800000;">"</span><span style="color:#800000;">admin</span><span style="color:#800000;">"</span><span style="color:#000000;">).FirstOrDefault(); Dept dept </span>= <span style="color:#0000ff;">new</span> Dept { ID = <span style="color:#800080;">1</span><span style="color:#000000;"> }; DB.Depts.Attach(dept); admin.Dept </span>=<span style="color:#000000;"> dept; DB.SaveChanges(); 如果实体已经被加载到EF上下文中,此时再次Attach同一个对象,就会出错: DB.Depts.Find(</span><span style="color:#800080;">1</span><span style="color:#000000;">); User admin </span>= DB.Users.Where(u => u.Name == <span style="color:#800000;">"</span><span style="color:#800000;">admin</span><span style="color:#800000;">"</span><span style="color:#000000;">).FirstOrDefault(); Dept dept </span>= <span style="color:#0000ff;">new</span> Dept { ID = <span style="color:#800080;">1</span><span style="color:#000000;"> }; DB.Depts.Attach(dept); admin.Dept </span>=<span style="color:#000000;"> dept; DB.SaveChanges(); 解决办法: 先在EF的Local缓存中查找对象,如果找不到再Attach新对象 EntityState 有以下状态 Added Deleted Modified Unchanged Detached eg </span><span style="color:#0000ff;">var</span> student = <span style="color:#0000ff;">new</span> Student() { StudentName = <span style="color:#800000;">"</span><span style="color:#800000;">New Student</span><span style="color:#800000;">"</span><span style="color:#000000;"> }; student.StudentAddress </span>= <span style="color:#0000ff;">new</span> StudentAddress() { Address1 = <span style="color:#800000;">"</span><span style="color:#800000;">Address</span><span style="color:#800000;">"</span>, City = <span style="color:#800000;">"</span><span style="color:#800000;">City1</span><span style="color:#800000;">"</span><span style="color:#000000;"> }; </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx =<span style="color:#000000;"> newSchoolDBEntities()) { ctx.Students.Attach(student); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 获取实体状态 EntityState</span> <span style="color:#0000ff;">var</span> studentEntry =<span style="color:#000000;"> ctx.Entry(disconnectedStudent); </span><span style="color:#0000ff;">var</span> addressEntry =<span style="color:#000000;"> ctx.Entry(disconnectedStudent.StudentAddress); Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">Student EntityState: {0}</span><span style="color:#800000;">"</span><span style="color:#000000;">,studentEntry.State); Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">StudentAddress EntityState: {0}</span><span style="color:#800000;">"</span><span style="color:#000000;">,addressEntry.State); } local https:</span><span style="color:#008000;">//</span><span style="color:#008000;">msdn.microsoft.com/en-us/data/jj592872</span> <span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> context = <span style="color:#0000ff;">new</span><span style="color:#000000;"> BloggingContext()) { </span><span style="color:#008000;">//</span><span style="color:#008000;"> Load all blogs from the database into the context </span> <span style="color:#000000;"> context.Blogs.Load(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> Add a new blog to the context </span> context.Blogs.Add(<span style="color:#0000ff;">new</span> Blog { Name = <span style="color:#800000;">"</span><span style="color:#800000;">My New Blog</span><span style="color:#800000;">"</span><span style="color:#000000;"> }); </span><span style="color:#008000;">//</span><span style="color:#008000;"> Mark one of the existing blogs as Deleted </span> context.Blogs.Remove(context.Blogs.Find(<span style="color:#800080;">1</span><span style="color:#000000;">)); </span><span style="color:#008000;">//</span><span style="color:#008000;"> Loop over the blogs in the context. </span> Console.WriteLine(<span style="color:#800000;">"</span><span style="color:#800000;">In Local: </span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">var</span> blog <span style="color:#0000ff;">in</span><span style="color:#000000;"> context.Blogs.Local) { Console.WriteLine( </span><span style="color:#800000;">"</span><span style="color:#800000;">Found {0}: {1} with state {2}</span><span style="color:#800000;">"</span><span style="color:#000000;">, blog.BlogId, blog.Name, context.Entry(blog).State); } </span><span style="color:#008000;">//</span><span style="color:#008000;"> Perform a query against the database. </span> Console.WriteLine(<span style="color:#800000;">"</span><span style="color:#800000;">\nIn DbSet query: </span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">var</span> blog <span style="color:#0000ff;">in</span><span style="color:#000000;"> context.Blogs) { Console.WriteLine( </span><span style="color:#800000;">"</span><span style="color:#800000;">Found {0}: {1} with state {2}</span><span style="color:#800000;">"</span><span style="color:#000000;">, blog.BlogId, blog.Name, context.Entry(blog).State); } } </span>----------------------------------------------<span style="color:#000000;"> 多表查询 </span>----------------------------------------------<span style="color:#000000;"> 多表关联查询 </span><span style="color:#0000ff;">var</span> q = (<span style="color:#0000ff;">from</span> exp <span style="color:#0000ff;">in</span><span style="color:#000000;"> Common.Db.RptImportExports join invest </span><span style="color:#0000ff;">in</span><span style="color:#000000;"> Common.Db.RptInvests on exp.Month equals invest.Month join consume </span><span style="color:#0000ff;">in</span><span style="color:#000000;"> Common.Db.RptConsumes on exp.Month equals consume.Month </span><span style="color:#0000ff;">select</span> <span style="color:#0000ff;">new</span><span style="color:#000000;"> { exp.Month, exp.ExpInc2, invest.LimitInvestInc, consume.LimitRetailInc2 }); </span><span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">this</span><span style="color:#000000;">.txtFrom.Text.IsNullOrEmpty()) q </span>= q.Where(t => t.Month.CompareTo(<span style="color:#0000ff;">this</span>.txtFrom.Text) >= <span style="color:#800080;">0</span><span style="color:#000000;">); </span><span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">this</span><span style="color:#000000;">.txtTo.Text.IsNullOrEmpty()) q </span>= q.Where(t => t.Month.CompareTo(<span style="color:#0000ff;">this</span>.txtTo.Text) <= <span style="color:#800080;">0</span><span style="color:#000000;">); 注意: join on 语句不能左右颠倒,会报错 q 是一个IQueryable</span><~a><span style="color:#000000;">匿名类,后面的Where语句会自动检测到Month属性 对应的Lambda表达式该怎么写? </span>----------------------------------------------<span style="color:#000000;"> 执行SQL </span>----------------------------------------------<span style="color:#000000;"> 返回实体 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx =<span style="color:#000000;"> newSchoolDBEntities()) { </span><span style="color:#008000;">//</span><span style="color:#008000;">列名必需要Entity属性匹配</span> <span style="color:#0000ff;">var</span> studentList = ctx.Students.SqlQuery(<span style="color:#800000;">"</span><span style="color:#800000;">Select * from Student</span><span style="color:#800000;">"</span>).ToList<Student><span style="color:#000000;">(); } 返回非实体类型 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx =<span style="color:#000000;"> newSchoolDBEntities()) { </span><span style="color:#008000;">//</span><span style="color:#008000;">Get student name of string type</span> <span style="color:#0000ff;">string</span> studentName = ctx.Database.SqlQuery<<span style="color:#0000ff;">string</span>>(<span style="color:#800000;">"</span><span style="color:#800000;">Select studentname from Student where studentid=1</span><span style="color:#800000;">"</span>).FirstOrDefault<<span style="color:#0000ff;">string</span>><span style="color:#000000;">(); } 执行SQL命令 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { </span><span style="color:#0000ff;">int</span> noOfRowUpdated = ctx.Database.ExecuteSqlCommand(<span style="color:#800000;">"</span><span style="color:#800000;">Update student set studentname ='changed student by command' where studentid=1</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">int</span> noOfRowInserted = ctx.Database.ExecuteSqlCommand(<span style="color:#800000;">"</span><span style="color:#800000;">insert into student(studentname) values('New Student')</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">int</span> noOfRowDeleted = ctx.Database.ExecuteSqlCommand(<span style="color:#800000;">"</span><span style="color:#800000;">delete from student where studentid=1</span><span style="color:#800000;">"</span><span style="color:#000000;">); } </span>----------------------------------------------<span style="color:#000000;"> 事务(Transaction) </span>----------------------------------------------<span style="color:#000000;"> 隐式事务 </span><span style="color:#0000ff;">try</span><span style="color:#000000;"> { Product prod1 </span>= db.Products.First(p => p.ProductID == <span style="color:#800080;">4</span><span style="color:#000000;">); Product prod2 </span>= db.Products.First(p => p.ProductID == <span style="color:#800080;">5</span><span style="color:#000000;">); prod1.UnitsInStock </span>-= <span style="color:#800080;">3</span><span style="color:#000000;">; prod2.UnitsInStock </span>-= <span style="color:#800080;">5</span>;<span style="color:#008000;">//</span><span style="color:#008000;"> 错误:库存 数量的单位不能是负数</span> db.SubmitChanges(); <span style="color:#008000;">//</span><span style="color:#008000;"> 要么全部成功要么全部失败</span> <span style="color:#000000;"> } </span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (System.Data.SqlClient.SqlExceptione) { </span><span style="color:#008000;">//</span><span style="color:#008000;">执行异常处理</span> <span style="color:#000000;"> } 明显式事务(TransactionScope) </span><span style="color:#0000ff;">using</span> (Transcope ts = <span style="color:#0000ff;">new</span><span style="color:#000000;"> TransactionScope()) { </span><span style="color:#0000ff;">try</span><span style="color:#000000;"> { Product prod1 </span>= db.Products.First(p => p.ProductID == <span style="color:#800080;">4</span><span style="color:#000000;">); Product prod2 </span>= db.Products.First(p => p.ProductID == <span style="color:#800080;">5</span><span style="color:#000000;">); prod1.UnitsInStock </span>-= <span style="color:#800080;">3</span><span style="color:#000000;">; prod2.UnitsInStock </span>-= <span style="color:#800080;">5</span>;<span style="color:#008000;">//</span><span style="color:#008000;"> 错误:库存 数量的单位不能是负数</span> db.SubmitChanges(); <span style="color:#008000;">//</span><span style="color:#008000;"> 要么全部成功要么全部失败</span> <span style="color:#000000;"> } </span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (System.Data.SqlClient.SqlExceptione) { </span><span style="color:#008000;">//</span><span style="color:#008000;">执行异常处理</span> <span style="color:#000000;"> } } </span>---------------------------- <span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> context = <span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogDbContext()) { </span><span style="color:#0000ff;">using</span> (TransactionScope transaction = <span style="color:#0000ff;">new</span><span style="color:#000000;"> TransactionScope()) { ...... context.SaveChanges(); ...... context.SaveChanges(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 提交事务。如果本语句前出现任何错误,所有操作都会回滚。</span> <span style="color:#000000;"> transaction.Complete(); } } </span>----------------------------------------------<span style="color:#000000;"> 缓存 </span>----------------------------------------------<span style="color:#000000;"> EF 只是对查询条件做了缓存,而不是缓存查询的结果集 在DbContext生命周期内,实体类是可以访问的。 无比怀念 Gentle.net 框架,它是在Application级别进行缓存的。 DbContext未关闭前 EF维护了若干实体的状态(Entry.State) 可以根据状态来判断实体是否有更新,若无更新即可直接调出来使用 强制不缓存(System.Data.Entity.DbExtensions.AsNoTracking) </span><span style="color:#0000ff;">var</span> people = context.People.Where(p => p.PersonID > <span style="color:#800080;">100</span><span style="color:#000000;">).AsNoTracking().ToList(); </span><span style="color:#0000ff;">var</span> monkey = <span style="color:#0000ff;">from</span> m <span style="color:#0000ff;">in</span><span style="color:#000000;"> _dataContext.Monkeys.AsNoTracking() </span><span style="color:#0000ff;">where</span> m.MonkeyId ==<span style="color:#000000;"> monkeyId </span><span style="color:#0000ff;">select</span><span style="color:#000000;"> m) .FirstOrDefault(); 强制从数据库中更新缓存 </span><span style="color:#0000ff;">var</span> service = (serviceAgent <span style="color:#0000ff;">as</span><span style="color:#000000;"> PersonAccountServiceAgent); </span><span style="color:#0000ff;">var</span> context =<span style="color:#000000;"> ((System.Data.Entity.Infrastructure.IObjectContextAdapter)service.DBConn.DataBase).ObjectContext; context.Refresh(System.Data.Objects.RefreshMode.StoreWins, CurrentAccount); EF老版本没有缓存的,不知道EF的缓存可不可靠,不放心的话用HttpContext的缓存,如 </span><span style="color:#0000ff;">if</span>( HttpContext.Cache[<span style="color:#800000;">"</span><span style="color:#800000;">temp</span><span style="color:#800000;">"</span>]==<span style="color:#0000ff;">null</span><span style="color:#000000;">) HttpContext.Cache[</span><span style="color:#800000;">"</span><span style="color:#800000;">temp</span><span style="color:#800000;">"</span>] = DataContext.info.<span style="color:#0000ff;">where</span>(p => p.id ==<span style="color:#000000;"> id).FirstOrDefault(); </span><span style="color:#0000ff;">return</span> HttpContext.Cache[<span style="color:#800000;">"</span><span style="color:#800000;">temp</span><span style="color:#800000;">"</span>] <span style="color:#0000ff;">as</span> List<Object><span style="color:#000000;">(); 使用 EntityFrame.Extended 有缓存处理 详见 EntityFrame.Extened 章节 </span>----------------------------------------------<span style="color:#000000;"> 并发处理 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.aichengxu.com/other/6164839.htm</span> ----------------------------------------------<span style="color:#000000;"> 并发处理 方案 悲观并发:A在读取某条记录时,就单独霸占了这个记录,B无法获</span>? <span style="color:#800080;">1</span>鬯屎鲜菥赫ち业某『希换岽此浪侍狻?<span style="color:#000000;"> 乐观并发:A</span>/<span style="color:#000000;">B都可以读取某条记录时,若A修改了,B提交修改时会提示(可选择覆盖或取消) 说明 乐观锁并不适合处理高并发的场景,少量的数据冲突才是乐观并发的初衷。 悲观锁同样也不适合处理高并发,特别在加锁成本比较大的时候。 如果项目并发量确实大, 那就可以考虑采用其他技术实现,比如 消息队列等 EF CodeFirst 只支持乐观并发 步骤 (</span><span style="color:#800080;">1</span><span style="color:#000000;">)添加RowVersion字段,类型为TimeStamp,且一个类只能有一个此标示 [Timestamp] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">byte</span> [] RowVersion { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } (</span><span style="color:#800080;">2</span><span style="color:#000000;">)EF更新实体时会先检查RowVersion,如果发现RowVersion不一致,则抛出DbUpdateConcurrencyException异常 (</span><span style="color:#800080;">3</span><span style="color:#000000;">)检测到异常后可考虑重加载数据 </span><span style="color:#008000;">//</span><span style="color:#008000;"> 创建一个ctx取的第一条数据,修改 但是不提交</span> BlogContext fircontext = <span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogContext(); Blog firstblog </span>=<span style="color:#000000;"> fircontext.Blogs.FirstOrDefault(); firstblog.BlogName </span>= <span style="color:#800000;">"</span><span style="color:#800000;">哈哈,被改掉了</span><span style="color:#800000;">"</span><span style="color:#000000;"> ; </span><span style="color:#008000;">//</span><span style="color:#008000;"> 创建另一个ctx还是取第一条数据,修改并提交</span> BlogContext secContext = <span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogContext(); Blog secondBlog </span>=<span style="color:#000000;"> secContext.Blogs.FirstOrDefault(); secondBlog.BlogAuthor </span>= <span style="color:#800000;">"</span><span style="color:#800000;">JasonShen</span><span style="color:#800000;">"</span><span style="color:#000000;">; secContext.SaveChanges(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 这个时候再提交第一个ctx所做的修改</span> <span style="color:#0000ff;">try</span><span style="color:#000000;"> { </span><span style="color:#008000;">//</span><span style="color:#008000;"> 这是后会发现现在的数据,已经和刚进入时发生了变化,故报错</span> <span style="color:#000000;"> fircontext.SaveChanges(); Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">保存成功</span><span style="color:#800000;">"</span><span style="color:#000000;"> ); } </span><span style="color:#0000ff;">catch</span><span style="color:#000000;">(Exception e) { Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">保存失败</span><span style="color:#800000;">"</span><span style="color:#000000;"> ); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 重新从数据库加载数据</span> <span style="color:#000000;"> e.Entries.Single().Reload(); Console.WriteLine(firstblog.BlogName); } 针对单个字段的并发 有些时候并不需要控制针对整条记录的并发,只需要控制某个列的数据 不会出现脏操作就ok 这个时候可使用ConcurrencyCheck 标示 </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> User { ... [ConcurrencyCheck ] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> CertID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } } 以下代码中会捕捉到并发异常 BlogContext firContext </span>= <span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogContext (); User u1 </span>=<span style="color:#000000;"> firContext.Users.FirstOrDefault(); BlogContext secContext </span>= <span style="color:#0000ff;">new</span><span style="color:#000000;"> BlogContext (); User u2 </span>=<span style="color:#000000;"> secContext.Users.FirstOrDefault(); u2.certID</span>= <span style="color:#800000;">"</span><span style="color:#800000;">22222</span><span style="color:#800000;">"</span><span style="color:#000000;">; secContext.SaveChanges(); </span><span style="color:#0000ff;">try</span><span style="color:#000000;"> { u1.certID </span>= <span style="color:#800000;">"</span><span style="color:#800000;">33333</span><span style="color:#800000;">"</span><span style="color:#000000;"> ; firContext.SaveChanges(); } </span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (Exception e) { Console .WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">并发报错</span><span style="color:#800000;">"</span><span style="color:#000000;"> ); } </span>----------------------------------------------<span style="color:#000000;"> 贪婪加载、懒惰加载、定向加载 </span>----------------------------------------------<span style="color:#000000;"> 惰性加载:延迟加载对象关联的实体,用到时再加载,EF默认为LazyLoading </span><span style="color:#0000ff;">var</span> students = ctx.Students.ToList<Student><span style="color:#000000;">(); </span><span style="color:#0000ff;">var</span> student = students[<span style="color:#800080;">0</span><span style="color:#000000;">]; </span><span style="color:#0000ff;">var</span> address = std.StudentAddress; <span style="color:#008000;">//</span><span style="color:#008000;"> 地址是动态加载的,此时才去查询</span> <span style="color:#000000;"> 贪婪加载:使用Include(),自动加载关联实体 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { </span><span style="color:#0000ff;">var</span> res = (<span style="color:#0000ff;">from</span> s <span style="color:#0000ff;">in</span> ctx.Students.Include(<span style="color:#800000;">"</span><span style="color:#800000;">Standard</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">where</span> s.StudentName == <span style="color:#800000;">"</span><span style="color:#800000;">Student1</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">select</span> s).FirstOrDefault<Student><span style="color:#000000;">(); } 定向加载:Reference()和Collection() 方法 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> ctx = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SchoolDBEntities()) { ctx.Configuration.LazyLoadingEnabled </span>= <span style="color:#0000ff;">false</span><span style="color:#000000;">; </span><span style="color:#008000;">//</span><span style="color:#008000;"> 加载学生</span> IList<Student> studList = ctx.Students.ToList<Student><span style="color:#000000;">(); Student std </span>= studList.Where(s => s.StudentID == <span style="color:#800080;">1</span>).FirstOrDefault<Student><span style="color:#000000;">(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 加载指定学生的Standard信息</span> ctx.Entry(std).Reference(s =><span style="color:#000000;"> s.Standard).Load(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> 加载指定学生的课程信息</span> ctx.Entry(std).Collection(s =><span style="color:#000000;"> s.Courses).Load(); } 禁用延迟加载 </span><span style="color:#0000ff;">public</span> PortalContext() : <span style="color:#0000ff;">base</span>(<span style="color:#800000;">"</span><span style="color:#800000;">name=PortalContext</span><span style="color:#800000;">"</span><span style="color:#000000;">) { </span><span style="color:#0000ff;">this</span>.Configuration.LazyLoadingEnabled = <span style="color:#0000ff;">false</span><span style="color:#000000;">; } </span>----------------------------------------------<span style="color:#000000;"> 修改部分字段不重建表 </span>----------------------------------------------<span style="color:#000000;"> 说明 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.tuicool.com/articles/EfIvey</span> <span style="color:#000000;"> 用了Codefirst后最大的困扰就是数据变化引起数据库的删除再新建,这样会造成数据丢失 在EntityFrameWork5.0中引入了数据迁移功能能很好的解决这个问题。 步骤 PM</span>> enable-<span style="color:#000000;">migrations 自动生成 migration配置文件类 </span><span style="color:#0000ff;">namespace</span><span style="color:#000000;"> NoteSystem.Migrations { </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Data.Entity; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Data.Entity.Migrations; </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> System.Linq; </span><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">sealed</span> <span style="color:#0000ff;">class</span> Configuration<TContext> : DbMigrationsConfiguration<TContext> <span style="color:#0000ff;">where</span><span style="color:#000000;"> TContext:DbContext { </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> Configuration() { AutomaticMigrationsEnabled </span>= <span style="color:#0000ff;">true</span><span style="color:#000000;">; AutomaticMigrationDataLossAllowed </span>= <span style="color:#0000ff;">true</span><span style="color:#000000;">; } } } 修改数据库构造函数 </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> XXXContext : DbContext { </span><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(</span><span style="color:#0000ff;">new</span> MigrateDatabaseToLatestVersion<NoteDb, Configuration<NoteDb>><span style="color:#000000;">()); </span><span style="color:#008000;">//</span><span style="color:#008000;">Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NoteDb>());</span> <span style="color:#000000;"> } } 对应的数据库中会多一个表 _MigrationHistory MigrationId ContextKey Model ProductVersion 每次修改Model代码后,会新增一条记录,并修改对应的表字段 注意,采用该方案后,不要手动去修改数据库字段。 </span>----------------------------------------------<span style="color:#000000;"> 查看 EF 生成的 SQL </span>----------------------------------------------<span style="color:#000000;"> (</span><span style="color:#800080;">1</span><span style="color:#000000;">) query.ToTraceString() (</span><span style="color:#800080;">2</span><span style="color:#000000;">) 调试时鼠标移到query上面,即可查看sql (</span><span style="color:#800080;">3</span><span style="color:#000000;">) 记录日志 </span><span style="color:#0000ff;">this</span>.Database.Log = (t) =><span style="color:#000000;"> Trace.WriteLine(t); http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.genshuixue.com/i-cxy/p/7689447</span> <span style="color:#000000;"> 记录到log: </span><interceptors> <interceptor type=<span style="color:#800000;">"</span><span style="color:#800000;">System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework</span><span style="color:#800000;">"</span>/> </interceptors><span style="color:#000000;"> 记录到文件 </span><interceptors> <interceptor type=<span style="color:#800000;">"</span><span style="color:#800000;">System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework</span><span style="color:#800000;">"</span>> <parameters> <parameter value=<span style="color:#800000;">"</span><span style="color:#800000;">C:\Temp\LogOutput.txt</span><span style="color:#800000;">"</span>/> </parameters> </interceptor> </interceptors><span style="color:#000000;"> (</span><span style="color:#800080;">4</span><span style="color:#000000;">) 通过 sqlserver 的 SQL Profile来查看EF生成和执行的SQL语句 </span>----------------------------------------------<span style="color:#000000;"> 外键关联 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.cnblogs.com/libingql/p/3353112.html</span> ----------------------------------------------<span style="color:#000000;"> 产品目录和产品 </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> Category { </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> CategoryID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> CategoryName { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> ICollection<Product> Products { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> Product { </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> ProductID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> ProductName { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">decimal</span> UnitPrice { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> CategoryID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> Category Category { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; } <span style="color:#008000;">//</span><span style="color:#008000;"> virtual是为了启用延迟加载</span> <span style="color:#000000;"> } 会自动生成 Product.CategoryID </span>---(N:<span style="color:#800080;">1</span>)---<span style="color:#000000;"> Category.CategoryID 删除Category某条记录会自动级联删除Product中的相关记录 手工指定外键 [ForeignKey(</span><span style="color:#800000;">"</span><span style="color:#800000;">UserProfile</span><span style="color:#800000;">"</span><span style="color:#000000;">)] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> UserID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } </span><span style="color:#008000;">//</span><span style="color:#008000;"> EF6</span> modelBuilder.Entity<Product><span style="color:#000000;">() .HasForeignKey(d </span>=> d.CatID) <span style="color:#008000;">//</span><span style="color:#008000;"> 外键</span> .HasRequired(t => t.Category) <span style="color:#008000;">//</span><span style="color:#008000;"> 外键值来自Category表</span> .WithMany(t => t.Products) <span style="color:#008000;">//</span><span style="color:#008000;"> Category对Product是1对多关系</span> <span style="color:#000000;"> ; </span><span style="color:#008000;">//</span><span style="color:#008000;"> EF7</span> modelBuilder.Entity<ChildEntity><span style="color:#000000;">() .ManyToOne(n </span>=> n.Entity, t =><span style="color:#000000;"> t.ChildEntities) .ForeignKey(t </span>=><span style="color:#000000;"> t.EntityId) ; modelBuilder.Entity</span><Entity><span style="color:#000000;">() .WithMany() .Map(x </span>=> x.MapKey(<span style="color:#800000;">"</span><span style="color:#800000;">ParentID</span><span style="color:#800000;">"</span><span style="color:#000000;">)) 禁止级联删除 modelBuilder.Entity</span><Product><span style="color:#000000;">() .HasForeignKey(d </span>=> d.CatID) <span style="color:#008000;">//</span><span style="color:#008000;"> 外键</span> .HasRequired(t => t.Category) <span style="color:#008000;">//</span><span style="color:#008000;"> 外键值来自Category表</span> .WithMany(t => t.Products) <span style="color:#008000;">//</span><span style="color:#008000;"> Category对Product是1对多关系</span> .WillCascadeOnDelete(<span style="color:#0000ff;">false</span>) <span style="color:#008000;">//</span><span style="color:#008000;"> 可禁止级联删除</span> <span style="color:#000000;"> ; 或统一禁止 </span><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove</span><PluralizingTableNameConvention>(); <span style="color:#008000;">//</span><span style="color:#008000;"> 禁用默认表名复数形式</span> modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); <span style="color:#008000;">//</span><span style="color:#008000;"> 禁用一对多级联删除</span> modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); <span style="color:#008000;">//</span><span style="color:#008000;"> 禁用多对多级联删除</span> <span style="color:#000000;"> } 更详细(或复杂的)请查看http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.cnblogs.com/libingql/p/3353112.html</span> <span style="color:#000000;"> 创建删除数据库 </span><span style="color:#0000ff;">string</span> userMDF = System.IO.Path.Combine(userTempFolder,  <span style="color:#800000;">@"</span><span style="color:#800000;">NewCreateDB.mdf</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">string</span> connStr = String.Format (<span style="color:#800000;">@"</span><span style="color:#800000;">DataSource=.SQLEXPRESS; AttachDbFilename={0};IntegratedSecurity=True; Connect Timeout=30;User Instance=True; Integrated Security = SSPI;</span><span style="color:#800000;">"</span><span style="color:#000000;">,userMDF); NewCreateDB newDB </span>= <span style="color:#0000ff;">new</span><span style="color:#000000;"> NewCreateDB(connStr); newDB.CreateDatabase(); ... </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> (db.DatabaseExists())    Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">Northwind数据库存在</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">else</span><span style="color:#000000;">   Console.WriteLine(</span><span style="color:#800000;">"</span><span style="color:#800000;">Northwind数据库不存在</span><span style="color:#800000;">"</span><span style="color:#000000;">); ..... newDB.DeleteDatabase(); 继承 [Table(Name </span>= <span style="color:#800000;">"</span><span style="color:#800000;">dbo.Contacts</span><span style="color:#800000;">"</span><span style="color:#000000;">)] [InheritanceMapping(Code </span>= <span style="color:#800000;">"</span><span style="color:#800000;">Unknown</span><span style="color:#800000;">"</span>, Type = <span style="color:#0000ff;">typeof</span>(Contact), IsDefault = <span style="color:#0000ff;">true</span><span style="color:#000000;">)] [InheritanceMapping(Code </span>= <span style="color:#800000;">"</span><span style="color:#800000;">Employee</span><span style="color:#800000;">"</span>, Type = <span style="color:#0000ff;">typeof</span><span style="color:#000000;">(EmployeeContact))] [InheritanceMapping(Code </span>= <span style="color:#800000;">"</span><span style="color:#800000;">Supplier</span><span style="color:#800000;">"</span>, Type = <span style="color:#0000ff;">typeof</span><span style="color:#000000;">(SupplierContact))] [InheritanceMapping(Code </span>= <span style="color:#800000;">"</span><span style="color:#800000;">Customer</span><span style="color:#800000;">"</span>, Type = <span style="color:#0000ff;">typeof</span><span style="color:#000000;">(CustomerContact))] [InheritanceMapping(Code </span>= <span style="color:#800000;">"</span><span style="color:#800000;">Shipper</span><span style="color:#800000;">"</span>, Type = <span style="color:#0000ff;">typeof</span><span style="color:#000000;">(ShipperContact))] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> Contact : INotifyPropertyChanging, INotifyPropertyChanged { [Column(Storage </span>= <span style="color:#800000;">"</span><span style="color:#800000;">_ContactID</span><span style="color:#800000;">"</span>,IsPrimaryKey = <span style="color:#0000ff;">true</span>, IsDbGenerated = <span style="color:#0000ff;">true</span><span style="color:#000000;">)] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span><span style="color:#000000;"> ContactID{ } [Column(Storage </span>= <span style="color:#800000;">"</span><span style="color:#800000;">_ContactType</span><span style="color:#800000;">"</span>,IsDiscriminator = <span style="color:#0000ff;">true</span><span style="color:#000000;">)] </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span><span style="color:#000000;"> ContactType{ } } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">abstract</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> FullContact : Contact{ } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> EmployeeContact : FullContact{ } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> SupplierContact : FullContact{ } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> CustomerContact : FullContact{ } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> ShipperContact : Contact{ }</pre> </div> <p> </p> <p> </p> <h1>EntityFramework 性能优化和三方辅助库</h1> <div class="cnblogs_code"> <pre>--------------------------------------------------<span style="color:#000000;"> 性能优化 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">www.cnblogs.com/nele/p/6118100.html</span> --------------------------------------------------<span style="color:#000000;"> 使用最新版EF nuget install</span>-<span style="color:#000000;">package EntityFramework 查看生成的 Sql 语句 VS 诊断工具 用SQL Server Profiler 工具监控运行的sql语句 数据库分页 </span><span style="color:#0000ff;">var</span> items = query.Skip((PageIndex - <span style="color:#800080;">1</span>) *<span style="color:#000000;"> PageSize).Take(PageSize).ToList(); 对只读数据不跟踪实体 </span><span style="color:#0000ff;">var</span> items = db.Person.Where(a => a.IsDeleted == <span style="color:#0000ff;">false</span><span style="color:#000000;">).AsNoTracking().ToList(); 对于只读操作,建议采用AsNoTracking,数据是Detached状态,不跟踪状态。 设置 Configuration.AutoDetectChangesEnabled </span>= <span style="color:#0000ff;">false</span>; <span style="color:#008000;">//</span><span style="color:#008000;"> 自动检测实体变更</span> Configuration.ValidateOnSaveEnabled = <span style="color:#0000ff;">false</span>; <span style="color:#008000;">//</span><span style="color:#008000;"> 保存时不验证数据有效性</span> Configuration.LazyLoadingEnabled = <span style="color:#0000ff;">false</span>; <span style="color:#008000;">//</span><span style="color:#008000;"> 禁止懒加载</span> Configuration.ProxyCreationEnabled = <span style="color:#0000ff;">false</span>; <span style="color:#008000;">//</span><span style="color:#008000;"> ?</span> <span style="color:#000000;"> 使用合适的加载方式 懒惰加载: 附加表字段不加载 context.Configuration.ProxyCreationEnabled </span>= <span style="color:#0000ff;">true</span><span style="color:#000000;">; context.Configuration.LazyLoadingEnabled </span>= <span style="color:#0000ff;">true</span><span style="color:#000000;">; 导航属性被标记为virtual 按需加载: 加载附加表字段,尽可能减少数据库访问次数 </span><span style="color:#0000ff;">var</span> user = db.Person.Include(a =><span style="color:#000000;"> a.Roles); ToList()的位置 IQueryable返回的是查询表达式,也就是说生成了SQL查询语句但是却还没有与数据库进行交互。 ToList()方法是将生成的 IQueryable 接口生成的 sql 语句提交给数据库进行查询,再转化为对象列表 db.Person.Where(a </span>=> a.IsDeleted == <span style="color:#0000ff;">false</span><span style="color:#000000;">).ToList(); 使用SqlQuery时不跟踪实体 此方法获得的实体查询是在数据库(Database)上,实体不会被上下文跟踪。 </span><span style="color:#0000ff;">var</span> user = db.Database.SqlQuery<User>(<span style="color:#800000;">"</span><span style="color:#800000;">select * from user</span><span style="color:#800000;">"</span><span style="color:#000000;">, parameter).ToList(); 此方法获得的实体查询是被上下文跟踪,所以能直接赋值后SaveChanges()。 </span><span style="color:#0000ff;">var</span> user = db.Set<User>().SqlQuery(<span style="color:#800000;">"</span><span style="color:#800000;">select * from user</span><span style="color:#800000;">"</span><span style="color:#000000;">).ToList(); user.Last().Name </span>= <span style="color:#800000;">"</span><span style="color:#800000;">makmong</span><span style="color:#800000;">"</span><span style="color:#000000;">; db.SaveChanges(); 尽量用ViewModel代替实体Model,减少获取字段 </span><span style="color:#0000ff;">var</span> query = db.User.Select(a => <span style="color:#0000ff;">new</span><span style="color:#000000;"> { Id </span>=<span style="color:#000000;"> a.Id, Name </span>=<span style="color:#000000;"> a.Name }).ToList(); </span>--------------------------------------------------<span style="color:#000000;"> Entity Framework Extendeds https:</span><span style="color:#008000;">//</span><span style="color:#008000;">github.com/loresoft/EntityFramework.Extended</span> nuget install-<span style="color:#000000;">package EntityFramework.Extended </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> EntityFramework.Extended; </span>--------------------------------------------------<span style="color:#000000;"> 直接删除 db.Users.Where(u </span>=> u.FirstName == <span style="color:#800000;">"</span><span style="color:#800000;">firstname</span><span style="color:#800000;">"</span><span style="color:#000000;">).Delete(); db.Users.Delete(u </span>=> u.FirstName == <span style="color:#800000;">"</span><span style="color:#800000;">firstName</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;"> 已废弃</span> <span style="color:#000000;"> 直接写更新语句 update user </span><span style="color:#0000ff;">set</span> name=<span style="color:#800000;">"</span><span style="color:#800000;">newName</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">where</span> firstName=<span style="color:#800000;">"</span><span style="color:#800000;">kevin</span><span style="color:#800000;">"</span><span style="color:#000000;">; db.Users.Where(t </span>=> t.FirstName==<span style="color:#800000;">"</span><span style="color:#800000;">Kevin</span><span style="color:#800000;">"</span>).Update(t => <span style="color:#0000ff;">new</span> User() {Name = <span style="color:#800000;">"</span><span style="color:#800000;">ceshi</span><span style="color:#800000;">"</span><span style="color:#000000;">}); db.Users.Update(t1 </span>=> t1.FirstName==<span style="color:#800000;">"</span><span style="color:#800000;">Kevin</span><span style="color:#800000;">"</span>, t2 => <span style="color:#0000ff;">new</span> User() {Name = <span style="color:#800000;">"</span><span style="color:#800000;">ceshi</span><span style="color:#800000;">"</span>}); <span style="color:#008000;">//</span><span style="color:#008000;"> 已废弃</span> <span style="color:#000000;"> 查询一批数据并统计个数 nuget install</span>-<span style="color:#000000;">package EntityFramework.Extended </span><span style="color:#0000ff;">var</span> q = db.User.Where(t => t.Name.StartsWith(<span style="color:#800000;">"</span><span style="color:#800000;">a</span><span style="color:#800000;">"</span><span style="color:#000000;">)); </span><span style="color:#0000ff;">var</span> q1 =<span style="color:#000000;"> q.FutureCount(); </span><span style="color:#0000ff;">var</span> q2 = q.Skip(<span style="color:#800080;">10</span>).Take(<span style="color:#800080;">10</span><span style="color:#000000;">).Future(); </span><span style="color:#0000ff;">var</span> data =<span style="color:#000000;"> q2.ToList(); </span><span style="color:#0000ff;">var</span> count =<span style="color:#000000;"> q1.Value; 注:FutureXXX()方法都暂停执行语句,等到第一个ToList()语句出现,批量执行? 所有Future函数中的查询包装到一个连接中执行 缓存 从缓存中取数据 </span><span style="color:#0000ff;">var</span> tasks = db.Tasks.Where(t => t.CompleteDate == <span style="color:#0000ff;">null</span>).FromCache(); <span style="color:#008000;">//</span><span style="color:#008000;"> 使用默认缓存</span> <span style="color:#0000ff;">var</span> users = db.User.Where(u => u.Id > <span style="color:#800080;">5</span>).FromCache(CachePolicy.WithDurationExpiration(TimeSpan.FromSeconds(<span style="color:#800080;">30</span>))); <span style="color:#008000;">//</span><span style="color:#008000;"> 300秒缓存</span> <span style="color:#000000;"> 可指定缓存标志 </span><span style="color:#008000;">//</span><span style="color:#008000;"> cache assigned tasks</span> <span style="color:#0000ff;">var</span> tasks =<span style="color:#000000;"> db.Tasks .Where(t </span>=> t.AssignedId == myUserId && t.CompleteDate == <span style="color:#0000ff;">null</span><span style="color:#000000;">) .FromCache(tags: </span><span style="color:#0000ff;">new</span>[] { <span style="color:#800000;">"</span><span style="color:#800000;">Task</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">Assigned-Task-</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> myUserId }); </span><span style="color:#008000;">//</span><span style="color:#008000;"> some update happened to Task, expire Task tag</span> CacheManager.Current.Expire(<span style="color:#800000;">"</span><span style="color:#800000;">Task</span><span style="color:#800000;">"</span><span style="color:#000000;">); 自定义缓存方案 Locator.Current.Register</span><ICacheProvider>(() => <span style="color:#0000ff;">new</span><span style="color:#000000;"> MemcachedProvider()); 移除缓存 RemoveCache() Audit 捕捉数据库实体变更。详见github </span><span style="color:#0000ff;">var</span> db = <span style="color:#0000ff;">new</span><span style="color:#000000;"> TrackerContext(); </span><span style="color:#0000ff;">var</span> audit =<span style="color:#000000;"> db.BeginAudit(); ... db.SaveChanges(); </span><span style="color:#0000ff;">var</span> log =<span style="color:#000000;"> audit.LastLog; </span>--------------------------------------------------<span style="color:#000000;"> EFUtilities https:</span><span style="color:#008000;">//</span><span style="color:#008000;">github.com/MikaelEliasson/EntityFramework.Utilities</span> nuget install-<span style="color:#000000;">package EFUtilities </span><span style="color:#0000ff;">using</span><span style="color:#000000;"> EntityFramework.Utilities; </span>--------------------------------------------------<span style="color:#000000;"> 修改 </span><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> db = <span style="color:#0000ff;">new</span><span style="color:#000000;"> YourDbContext()) { db.AttachAndModify(</span><span style="color:#0000ff;">new</span> BlogPost { ID = postId }).Set(x => x.Reads, <span style="color:#800080;">10</span><span style="color:#000000;">); db.SaveChanges(); } IncludeEFU </span><span style="color:#0000ff;">var</span> result =<span style="color:#000000;"> db.Contacts .IncludeEFU(db, c </span>=><span style="color:#000000;"> c.PhoneNumbers) .ToList(); </span><span style="color:#0000ff;">var</span> result =<span style="color:#000000;"> db.Contacts .IncludeEFU(db, x </span>=><span style="color:#000000;"> x.PhoneNumbers .Where(n </span>=> n.Number == <span style="color:#800000;">"</span><span style="color:#800000;">10134</span><span style="color:#800000;">"</span><span style="color:#000000;">) .OrderBy(p </span>=><span style="color:#000000;"> p.ContactId) .ThenByDescending(p </span>=><span style="color:#000000;"> p.Number)) .ToList() ; ForceDelete db.Database.ForceDelete() Bulk delete </span><span style="color:#0000ff;">var</span> count =<span style="color:#000000;"> EFBatchOperation .For(db, db.BlogPosts) .Where(b </span>=> b.Created < upper && b.Created > lower && b.Title == <span style="color:#800000;">"</span><span style="color:#800000;">T2.0</span><span style="color:#800000;">"</span><span style="color:#000000;">) .Delete() ; Bulk insert EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); Batch update EFBatchOperation.For(db, db.Comments).UpdateAll(commentsFromDb, x </span>=> x.ColumnsToUpdate(c =><span style="color:#000000;"> c.Reads)); </span><span style="color:#0000ff;">var</span> lines = csv.ReadAllLines().Select(l => l.Split(<span style="color:#800000;">"</span><span style="color:#800000;">;</span><span style="color:#800000;">"</span><span style="color:#000000;">)); </span><span style="color:#0000ff;">var</span> comments = lines.Select(line => <span style="color:#0000ff;">new</span> Comment{ Id = <span style="color:#0000ff;">int</span>.Parse(line[<span style="color:#800080;">0</span>]), Reads = <span style="color:#0000ff;">int</span>.Parse(line[<span style="color:#800080;">1</span><span style="color:#000000;">]) }); EFBatchOperation.For(db, db.Comments).UpdateAll(comments, x </span>=> x.ColumnsToUpdate(c =><span style="color:#000000;"> c.Reads)); EFBatchOperation.For(db, db.Comments).Where(x </span>=> x.Text == <span style="color:#800000;">"</span><span style="color:#800000;">a</span><span style="color:#800000;">"</span>).Update(x => x.Reads, x => x.Reads + <span style="color:#800080;">1</span><span style="color:#000000;">); </span>--------------------------------------------------<span style="color:#000000;"> LinqKit https:</span><span style="color:#008000;">//</span><span style="color:#008000;">github.com/loresoft/EntityFramework.Extended</span> -------------------------------------------------- --------------------------------------------------<span style="color:#000000;"> LinqToExcel https:</span><span style="color:#008000;">//</span><span style="color:#008000;">github.com/paulyoder/LinqToExcel</span> nuget install-<span style="color:#000000;">package LinqToExcel </span>--------------------------------------------------<span style="color:#000000;"> demo </span><span style="color:#0000ff;">var</span> excel = <span style="color:#0000ff;">new</span> ExcelQueryFactory(<span style="color:#800000;">"</span><span style="color:#800000;">excelFileCsvFile</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> excel.Worksheet<Company><span style="color:#000000;">() </span><span style="color:#0000ff;">where</span> c.State == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; worksheet </span><span style="color:#0000ff;">var</span> oldCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> repo.Worksheet<Company>(<span style="color:#800000;">"</span><span style="color:#800000;">US Companies</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;">worksheet name = 'US Companies'</span> <span style="color:#0000ff;">where</span> c.LaunchDate < <span style="color:#0000ff;">new</span> DateTime(<span style="color:#800080;">1900</span>, <span style="color:#800080;">1</span>, <span style="color:#800080;">1</span><span style="color:#000000;">) </span><span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 字段映射 excel.AddMapping</span><Company>(x => x.State, <span style="color:#800000;">"</span><span style="color:#800000;">Providence</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;">maps the "State" property to the "Providence" column</span> excel.AddMapping(<span style="color:#800000;">"</span><span style="color:#800000;">Employees</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">Employee Count</span><span style="color:#800000;">"</span>); <span style="color:#008000;">//</span><span style="color:#008000;">maps the "Employees" property to the "Employee Count" column</span> <span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> excel.Worksheet<Company><span style="color:#000000;">() </span><span style="color:#0000ff;">where</span> c.State == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> && c.Employees > <span style="color:#800080;">500</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 或者 </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> Company { [ExcelColumn(</span><span style="color:#800000;">"</span><span style="color:#800000;">Company Title</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;">maps the "Name" property to the "Company Title" column</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Name { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } [ExcelColumn(</span><span style="color:#800000;">"</span><span style="color:#800000;">Providence</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;">maps the "State" property to the "Providence" column</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> State { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } [ExcelColumn(</span><span style="color:#800000;">"</span><span style="color:#800000;">Employee Count</span><span style="color:#800000;">"</span>)] <span style="color:#008000;">//</span><span style="color:#008000;">maps the "Employees" property to the "Employee Count" column</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Employees { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span><span style="color:#000000;">; } } </span><span style="color:#0000ff;">where</span> <span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span><span style="color:#000000;"> excel.Worksheet() </span><span style="color:#0000ff;">where</span> c[<span style="color:#800000;">"</span><span style="color:#800000;">State</span><span style="color:#800000;">"</span>] == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> || c[<span style="color:#800000;">"</span><span style="color:#800000;">Zip</span><span style="color:#800000;">"</span>] == <span style="color:#800000;">"</span><span style="color:#800000;">46550</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; </span><span style="color:#0000ff;">var</span> largeCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span><span style="color:#000000;"> excel.Worksheet() </span><span style="color:#0000ff;">where</span> c[<span style="color:#800000;">"</span><span style="color:#800000;">EmployeeCount</span><span style="color:#800000;">"</span>].Cast<<span style="color:#0000ff;">int</span>>() > <span style="color:#800080;">500</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 无标题行 </span><span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span><span style="color:#000000;"> excel.WorksheetNoHeader() </span><span style="color:#0000ff;">where</span> c[<span style="color:#800080;">2</span>] == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> <span style="color:#008000;">//</span><span style="color:#008000;">value in 3rd column</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 在Excel命名区域(NamedRange)中查找 </span><span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> excel.NamedRange<Company>(<span style="color:#800000;">"</span><span style="color:#800000;">NamedRange</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;">Selects data within the range named 'NamedRange'</span> <span style="color:#0000ff;">where</span> c.State == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 在Excel指定区域中查找 </span><span style="color:#0000ff;">var</span> indianaCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> excel.WorksheetRange<Company>(<span style="color:#800000;">"</span><span style="color:#800000;">B3</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">G10</span><span style="color:#800000;">"</span>) <span style="color:#008000;">//</span><span style="color:#008000;">Selects data within the B3 to G10 cell range</span> <span style="color:#0000ff;">where</span> c.State == <span style="color:#800000;">"</span><span style="color:#800000;">IN</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 根据索引找工作表 </span><span style="color:#0000ff;">var</span> oldCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> repo.Worksheet<Company>(<span style="color:#800080;">1</span>) <span style="color:#008000;">//</span><span style="color:#008000;">Queries the second worksheet in alphabetical order</span> <span style="color:#0000ff;">where</span> c.LaunchDate < <span style="color:#0000ff;">new</span> DateTime(<span style="color:#800080;">1900</span>, <span style="color:#800080;">1</span>, <span style="color:#800080;">1</span><span style="color:#000000;">) </span><span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 字段处理 excel.AddTransformation</span><Company>(x => x.IsBankrupt, cellValue => cellValue == <span style="color:#800000;">"</span><span style="color:#800000;">Y</span><span style="color:#800000;">"</span><span style="color:#000000;">); </span><span style="color:#0000ff;">var</span> bankruptCompanies = <span style="color:#0000ff;">from</span> c <span style="color:#0000ff;">in</span> excel.Worksheet<Company><span style="color:#000000;">() </span><span style="color:#0000ff;">where</span> c.IsBankrupt == <span style="color:#0000ff;">true</span> <span style="color:#0000ff;">select</span><span style="color:#000000;"> c; 获取工作表名</span>/<span style="color:#000000;">字段名 </span><span style="color:#0000ff;">var</span> worksheetNames =<span style="color:#000000;"> excel.GetWorksheetNames(); </span><span style="color:#0000ff;">var</span> columnNames = excel.GetColumnNames(<span style="color:#800000;">"</span><span style="color:#800000;">worksheetName</span><span style="color:#800000;">"</span><span style="color:#000000;">); 一些全局设置 excel.TrimSpaces </span>=<span style="color:#000000;"> TrimSpacesType.Both; excel.ReadOnly </span>= <span style="color:#0000ff;">true</span><span style="color:#000000;">; excel.DatabaseEngine </span>==<span style="color:#000000;"> DatabaseEngine.Ace; excel.StrictMapping </span>=<span style="color:#000000;"> StrictMappingType.Both; excel.UsePersistentConnection </span>= <span style="color:#0000ff;">true</span>;</pre> </div> <p> </p> <h1>EntityFramework & Expression</h1> <div class="cnblogs_code"> <pre>------------------------------------------------<span style="color:#000000;"> Expression 表达式(二叉树) 参考 http:</span><span style="color:#008000;">//</span><span style="color:#008000;">kb.cnblogs.com/page/42489/</span> http:<span style="color:#008000;">//</span><span style="color:#008000;">kb.cnblogs.com/page/42509/5/</span> http:<span style="color:#008000;">//</span><span style="color:#008000;">kb.cnblogs.com/page/42509/</span> ------------------------------------------------<span style="color:#000000;"> eg Expression</span><Func<<span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>>> expression = (a, b) => a * b + <span style="color:#800080;">2</span><span style="color:#000000;">; </span><span style="color:#0000ff;">var</span> lambda =<span style="color:#000000;"> expression.Compile(); </span><span style="color:#0000ff;">var</span> result = lambda(<span style="color:#800080;">1</span>, <span style="color:#800080;">2</span>); <span style="color:#008000;">//</span><span style="color:#008000;"> 4</span> <span style="color:#000000;"> eg: </span><span style="color:#800080;">2</span>+<span style="color:#800080;">3</span><span style="color:#000000;"> BinaryExpression body </span>= Expression.Add(Expression.Constant(<span style="color:#800080;">2</span>), Expression.Constant(<span style="color:#800080;">3</span>)); <span style="color:#008000;">//</span><span style="color:#008000;"> 2+3</span> Expression<Func<<span style="color:#0000ff;">int</span>>> expression = Expression.Lambda<Func<<span style="color:#0000ff;">int</span>>>(body, <span style="color:#0000ff;">null</span><span style="color:#000000;">); Expression </span>-><span style="color:#000000;"> Lambda Func</span><<span style="color:#0000ff;">int</span>> lambda =<span style="color:#000000;"> expression.Compile(); Console.WriteLine(lambda()); 概念 表达式树主要由下面四部分组成: </span><span style="color:#800080;">1</span><span style="color:#000000;">、Body 主体部分 </span><span style="color:#800080;">2</span><span style="color:#000000;">、Parameters 参数部分 </span><span style="color:#800080;">3</span><span style="color:#000000;">、NodeType 节点类型 </span><span style="color:#800080;">4</span><span style="color:#000000;">、Lambda表达式类型 eg Expression</span><Func<<span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>>> expr = (x, y) => { <span style="color:#0000ff;">return</span> x+<span style="color:#000000;">y; }; 等价于 ParameterExpression p1 </span>= Expression.Parameter(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">int</span>), <span style="color:#800000;">"</span><span style="color:#800000;">a</span><span style="color:#800000;">"</span><span style="color:#000000;">); ParameterExpression p2 </span>= Expression.Parameter(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">int</span>), <span style="color:#800000;">"</span><span style="color:#800000;">b</span><span style="color:#800000;">"</span><span style="color:#000000;">); BinaryExpression exp </span>=<span style="color:#000000;"> Expression.Multiply(p1, p2); </span><span style="color:#0000ff;">var</span> lamExp = Expression.Lambda<Func<<span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>>>(exp, <span style="color:#0000ff;">new</span><span style="color:#000000;"> ParameterExpression[] { p1, p2 }); 常见的表达式(详见 System.Linq.Expression类) 注:表达式常有Assign、Checked附加重载,表示是否检测功能,详见示例 表达式都有以下类别(覆盖了所有的C#表达式) BinaryExpression(二元表达式) Add</span>/Substract/Multiply/Devide/Power : +-*/^<span style="color:#000000;"> Equal</span>/NotEqual/ReferenceEqual/ReferenceNotEqual : == !=<span style="color:#000000;"> GreaterThan</span>/GreaterThanOrEqual/LessThan/LessThanOrEqual : > >= < <=<span style="color:#000000;"> And</span>/Or/OrElse/ExclusiveOr : && ||<span style="color:#000000;"> Assign : Coalesce : LeftShift</span>/RightShift : << >><span style="color:#000000;"> MakeBinary : Modulo</span>/<span style="color:#000000;">ModuloAssign : MethodCallExpression IndexExpression UnaryExpression(单元表达式) Increment</span>/PreIncrement/PostIncrement : ++<span style="color:#000000;"> Decrement</span>/PreDecrement/PostDecrement : --<span style="color:#000000;"> IsFalse</span>/IsTrue : ??<span style="color:#000000;"> Not : </span>!<span style="color:#000000;"> Negate : </span>-<span style="color:#000000;"> Quote : </span><span style="color:#800000;">" </span> Throw/<span style="color:#000000;">Rethrow : TypeAs : </span><span style="color:#0000ff;">as</span><span style="color:#000000;"> UnaryPlus : Unbox : </span>!<span style="color:#000000;"> OnesComplement : MakeUnary : Convert : BlockExpression GotoExpression DebugInfoExpression ConditionalExpression ConstantExpression DefaultExpression DynamicExpression InvocationExpression LabelExpression LambdaExpression ListInitExpression LoopExpression TryExpression MemberInitExpression NewExpression NewArrayExpression ParameterExpression RuntimeVariablesExpression SwitchExpression TypeBinaryExpression 常见的表达式 数学相关 Add</span>/Substract/Multiply/Devide/<span style="color:#000000;">Power Equal</span>/NotEqual/ReferenceEqual/<span style="color:#000000;">ReferenceNotEqual GreaterThan</span>/GreaterThanOrEqual/LessThan/<span style="color:#000000;">LessThanOrEqual 布尔相关 Equal</span>/NotEqual/ReferenceEqual/<span style="color:#000000;">ReferenceNotEqual GreaterThan</span>/GreaterThanOrEqual/LessThan/<span style="color:#000000;">LessThanOrEqual And</span>/Or/OrElse/<span style="color:#000000;">ExclusiveOr 反射相关 Constant Property Field Call 数组相关 ArrayAccess</span>/ArrayIndex/ArrayLength/<span style="color:#000000;"> ExpressionVisitor 若表达式为加法,改为减法 </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> OperationsVisitor : ExpressionVisitor { </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> Expression Modify(Expression expression) { </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> Visit(expression); } </span><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span><span style="color:#000000;"> Expression VisitBinary(BinaryExpression b) { </span><span style="color:#0000ff;">if</span> (b.NodeType ==<span style="color:#000000;"> ExpressionType.Add) { Expression left </span>= <span style="color:#0000ff;">this</span><span style="color:#000000;">.Visit(b.Left); Expression right </span>= <span style="color:#0000ff;">this</span><span style="color:#000000;">.Visit(b.Right); </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> Expression.Subtract(left,right); } </span><span style="color:#0000ff;">return</span> <span style="color:#0000ff;">base</span><span style="color:#000000;">.VisitBinary(b); } } a</span>+b*<span style="color:#800080;">2</span> 变为 a-b*<span style="color:#000000;">x Expression</span><Func<<span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">int</span>>> lambda = (a, b) => a + b * <span style="color:#800080;">2</span><span style="color:#000000;">; </span><span style="color:#0000ff;">var</span> operationsVisitor = <span style="color:#0000ff;">new</span><span style="color:#000000;"> OperationsVisitor(); Expression modifyExpression </span>=<span style="color:#000000;"> operationsVisitor.Modify(lambda); Console.WriteLine(modifyExpression.ToString()); IQueryable http:</span><span style="color:#008000;">//</span><span style="color:#008000;">kb.cnblogs.com/page/42510/</span> EF中查询表达式(linq <span style="color:#0000ff;">for</span><span style="color:#000000;"> sql 或 lamdba)返回的结果是IQueryable </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span><span style="color:#000000;"> IQueryable : IEnumerable { Type ElementType {</span><span style="color:#0000ff;">get</span><span style="color:#000000;">;} Expression Expression {</span><span style="color:#0000ff;">get</span><span style="color:#000000;">;} IQueryProvider Provider {</span><span style="color:#0000ff;">get</span><span style="color:#000000;">;} } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> IQueryable<T> : IQueryable, IEnumerable<T><span style="color:#000000;">, IEnumerable { } </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span><span style="color:#000000;"> IQueryProvider { IQueryable CreateQuery(Expressiong expression); </span><span style="color:#0000ff;">object</span><span style="color:#000000;"> Execute(Expression expression); IQueryable</span><T> CreateQuery<T>(Expression expression)<span style="color:#800000;">' </span> T Execute<T><span style="color:#000000;">(Expression expression); } 查询数组 List</span><String> myList = <span style="color:#0000ff;">new</span> List<String>() { <span style="color:#800000;">"</span><span style="color:#800000;">a</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">ab</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">cd</span><span style="color:#800000;">"</span>, <span style="color:#800000;">"</span><span style="color:#800000;">bd</span><span style="color:#800000;">"</span><span style="color:#000000;"> }; IEnumerable</span><String> query = <span style="color:#0000ff;">from</span> s <span style="color:#0000ff;">in</span><span style="color:#000000;"> myList </span><span style="color:#0000ff;">where</span> s.StartsWith(<span style="color:#800000;">"</span><span style="color:#800000;">a</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">select</span><span style="color:#000000;"> s; </span><span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">string</span> s <span style="color:#0000ff;">in</span><span style="color:#000000;"> query) ....; Expression </span><span style="color:#0000ff;">in</span><span style="color:#000000;"> linq to sql </span><span style="color:#0000ff;">select</span><span style="color:#000000;"> contactName IQueryable</span><Customer> custs =<span style="color:#000000;">db.Customers; ParameterExpression param </span>= Expression.Parameter(<span style="color:#0000ff;">typeof</span> (Customer), <span style="color:#800000;">"</span><span style="color:#800000;">c</span><span style="color:#800000;">"</span><span style="color:#000000;">); Expression selector </span>=Expression.Property(param, <span style="color:#0000ff;">typeof</span> (Customer).GetProperty(<span style="color:#800000;">"</span><span style="color:#800000;">ContactName</span><span style="color:#800000;">"</span><span style="color:#000000;">)); Expression pred </span>=<span style="color:#000000;">Expression.Lambda(selector, param); Expression expr </span>= Expression.Call(<span style="color:#0000ff;">typeof</span>(Queryable), <span style="color:#800000;">"</span><span style="color:#800000;">Select</span><span style="color:#800000;">"</span>, <span style="color:#0000ff;">new</span> Type[] { <span style="color:#0000ff;">typeof</span> (Customer), <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">string</span><span style="color:#000000;">) }, Expression.Constant(custs), pred); IQueryable</span><<span style="color:#0000ff;">string</span>> query = db.Customers.AsQueryable().Provider.CreateQuery<<span style="color:#0000ff;">string</span>><span style="color:#000000;">(expr); 生成的 SQL语句为: SELECT [t0].[ContactName] FROM [dbo]. [Customers]AS [t0] </span>----------------------------------------------------------------<span style="color:#000000;"> Where IQueryable</span><Customer> custs =<span style="color:#000000;"> db.Customers; ParameterExpression param </span>= Expression.Parameter(<span style="color:#0000ff;">typeof</span>(Customer), <span style="color:#800000;">"</span><span style="color:#800000;">c</span><span style="color:#800000;">"</span><span style="color:#000000;">); Expression left </span>= Expression.Property(param, <span style="color:#0000ff;">typeof</span>(Customer).GetProperty(<span style="color:#800000;">"</span><span style="color:#800000;">City</span><span style="color:#800000;">"</span>)); <span style="color:#008000;">//</span><span style="color:#008000;">c.City=="London"</span> Expression right = Expression.Constant(<span style="color:#800000;">"</span><span style="color:#800000;">London</span><span style="color:#800000;">"</span><span style="color:#000000;">); Expression filter </span>=<span style="color:#000000;"> Expression.Equal(left, right); Expression pred </span>=<span style="color:#000000;"> Expression.Lambda(filter, param); Expression expr </span>= Expression.Call(<span style="color:#0000ff;">typeof</span>(Queryable), <span style="color:#800000;">"</span><span style="color:#800000;">Where</span><span style="color:#800000;">"</span>, <span style="color:#0000ff;">new</span> Type[] { <span style="color:#0000ff;">typeof</span>(Customer) }, Expression.Constant(custs), pred); <span style="color:#008000;">//</span><span style="color:#008000;">Where(c=>c.City=="London")</span> IQueryable<Customer> query = db.Customers.AsQueryable().Provider.CreateQuery<Customer><span style="color:#000000;">(expr); 生成的SQL语句为: SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] </span>= @p0</pre> </div> <p> </p> </div> </div> </div> </div> </div> </div> <!--PC和WAP自适应版--> <div id="SOHUCS" sid="1305219062061633536"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- 文章页-底部 动态广告位 --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- 文章页-右侧1 动态广告位 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- 文章页-右侧2 动态广告位 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- 文章页-右侧3 动态广告位 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(EntityFramwork 七七八八)</h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/1835430939560341504.htm" title="人要有自知之明" target="_blank">人要有自知之明</a> <span class="text-muted">孟冬廿六</span> <div>今天中午跟一学妹聊天,谈起结婚找对象的问题,小姑娘年龄不算大,二十七岁,但是整个人很清醒很现实,她如今在一国企上班,吃住都不花钱,再加上她经常出差,补助奖金这一块儿也不少,一年下来七七八八的有个小二十万,这对于一个小姑娘来说已经非常不错了,她计划这两年自己付首付买房,然后想要买辆MINI,小姑娘一米七六的个子,长得漂亮有气质,家庭条件也不错,所以对于择偶方面也有一定的要求,最好是事业单位的,父母有</div> </li> <li><a href="/article/1831122485286891520.htm" title="我的罗技K480键盘进水了,但我度过了此次危机" target="_blank">我的罗技K480键盘进水了,但我度过了此次危机</a> <span class="text-muted">離岸海霧</span> <div>我的罗技K480进水了,我把小半杯红茶扣在了上面。喝红茶的第三天,我终于把它给扣了。红茶一瞬间就渗进了每个按键的缝隙,连旋钮都被浸泡了,放置ipad的卡槽也积了一汪水。绝望时刻。倒吸一口凉气,我首先抢救的是iPad。擦干了iPad我才想起要给键盘断电,捞起键盘关闭了背面的开关,好在键盘背面没湿。接下来就是抢救键盘的过程,整体气氛十分被动。伸手连抽五张纸巾,把表面的水吸掉了七七八八。提起键盘倾倒,水</div> </li> <li><a href="/article/1829037005233811456.htm" title="新尝试新挑战" target="_blank">新尝试新挑战</a> <span class="text-muted">棉花小糖猫</span> <div>这周时间过得太匆忙,感觉好像什么都没做,仿佛被时间忽略过。大概心里惦记的七七八八的事太多,脑子里总有未完成事项。我倒是硬性把时间切开了,并没有做这件事的时候脑子里想那件事。这是唯一的好处吧。说来这两天在画一副画,给父母搬家稳居画的,决定挑战一下从没画过的类型。虽然是在网上找了一个视频,但是没有颜料颜色的介绍,只能靠自己猜,而且有种笔从没用过,照着买来的,看似一样,一蘸颜料就全完蛋了,完全不是那回事</div> </li> <li><a href="/article/1828455723751796736.htm" title="2019-02-27" target="_blank">2019-02-27</a> <span class="text-muted">海眠君</span> <div>今天又和朋友聊起了关于选择,就叫她T吧。今天下午风很大,可以说一直是狂风大作,吹的我头疼,然后就在寝室窝了一个下午。后来回到教室T跟我说关于之前的那个补习班,问我还去不去。刚开始我有点不懂,因为之前我已经算是做出了决定,可是跟他聊了之后觉得心里又像是一团麻线。之后我们先后说到了关于决定来这所学校的原因和各种七七八八的。多半都是她在说,我在听。我就这样耳朵听着她温柔的声音,眼睛盯着她的眼睛,随着她的</div> </li> <li><a href="/article/1827313272827768832.htm" title="卖了一些纸箱和空瓶子" target="_blank">卖了一些纸箱和空瓶子</a> <span class="text-muted">德纯西西</span> <div>前段时间总喜欢在网上买一些七七八八,拆了快递之后就会有一些纸盒子,听说全球纸都涨价了,所以就把快递盒纸箱子,空水瓶都留下来积攒到一定的数量之后卖掉,今天就去附近的回收站给卖掉了,一大堆卖了5块钱,然后买了2注彩票,一张体彩大乐透,一张福彩双色球哈哈,幻想有时候也会让人感到一阵快乐。</div> </li> <li><a href="/article/1826387214288908288.htm" title="凡人俗事(17)" target="_blank">凡人俗事(17)</a> <span class="text-muted">敏儿好学300</span> <div>2021.8.17星期二多云昨天刚刚听说一些年轻老师调走了,今天社区值守时无意中大家也谈到了这个话题。事情发生在交接班前一刻钟。当时正聊到开学前的准备,其他三人好像百事通一样接话说中年级走了好几个人。于是,各种八卦猜测齐上阵,基本上也能把事情猜个七七八八了。这些年,从我们学校调出的老师真是不少,都让人有种为他人做嫁衣的感觉。明明来的时候都不咋地,经过学校的大力培养,稍有成绩挥挥衣袖就像天边的云彩飘</div> </li> <li><a href="/article/1773537930497818624.htm" title="浙江女大学生“卖淫日记”曝光,震惊全网:背后还藏着一个真相" target="_blank">浙江女大学生“卖淫日记”曝光,震惊全网:背后还藏着一个真相</a> <span class="text-muted">地球上的星星_272e</span> <div>作者/在风来源/微信公众号:自黑思维最近,一个新闻震碎无数人的三观:在浙江农林大学,有一位女大学生,在网上公开自己的“卖淫日记”,无论是文字还是图片,都非常露骨。而目前关于这事的瓜,都已经被删得七七八八,但我有保留截图的习惯,所以才有了这篇文章。首先,必须要强调的一点,就是这事不是谣传,是真的,学校已经出来证实过。只是学校表示该名女学生有精神病。但到底是不是真的精神有问题?我们可以从她的日记里,略</div> </li> <li><a href="/article/1772028030022123520.htm" title="互联网年轻人创业项目 10个互联网创业项目" target="_blank">互联网年轻人创业项目 10个互联网创业项目</a> <span class="text-muted">配音新手圈</span> <div>如果你想小成本创业,那现在像是什么餐饮店、培训班、奶茶加盟之类的就不用怎么考虑了。兼职副业推荐公众号,配音新手圈,声优配音圈,新配音兼职圈,配音就业圈,鼎音副业,有声新手圈,每天更新各种远程工作与在线兼职,职位包括:写手、程序开发、剪辑、设计、翻译、配音、无门槛、插画、翻译、等等。。。每日更新兼职。这些没个十几二十几万是做不起来的。门面费、水电费、加盟费、培训费...反正七七八八的费用都不少。现在</div> </li> <li><a href="/article/1759897651463155712.htm" title="一地鸡毛—一个中年男人的日常82" target="_blank">一地鸡毛—一个中年男人的日常82</a> <span class="text-muted">随止心语所自欲律</span> <div>2020年5月21日,星期四,多云。昨晚凌晨三点半才睡,躺床上写了一个小时的小说才写了八百字,以后再也不敢吹牛说日更五千字了。早上起床头晕脑胀,到单位以后,屁股刚挨着凳子,就有源源不断的工作迎面袭来,忙得都没时间锻炼身体,一直忙到了下班。才把工作忙得七七八八。中午的西红柿鸡蛋面很好吃,吃完以后再也不想动弹了,躺床上赶快午休。一觉睡到了2:20,闹钟都没有把我叫醒下午上班时感觉工作很多,但是没有个头</div> </li> <li><a href="/article/1759859666239713280.htm" title="一切都会越来越好的" target="_blank">一切都会越来越好的</a> <span class="text-muted">一仰望天空的鱼</span> <div>周三的时候,我们商定周六一定要聚会,终于,我们相聚了。朋友们聚在一起总是说不完的话,不知不觉天黑了,不得不分别。坐在末班车上,看着这个城市色彩纷呈的灯光,听着车里抒情的歌曲,心里满满的,说不出来什么滋味。有朋友相聚的知足,亦有分别的不舍,更有相识十几年来的回忆。这一天,我们没有停止说话,说着自己的快乐与惆怅,掺杂着别人的七七八八,时间过得飞快。每个人都有了新的收获,或多或少,都是值得为之高兴的。有</div> </li> <li><a href="/article/1759771604092547072.htm" title="第一次做核酸" target="_blank">第一次做核酸</a> <span class="text-muted">青竹筠</span> <div>今天一整天都在医院陪公公做入院前的各项检查,七七八八加起来有十几个项目。早上需要空腹抽血和做彩超,每一个项目都得排队半小时左右,感叹医院真忙。有些是子女陪年迈的老人来看病的,也有两个古稀老人互相陪伴搀扶着来的。有那么一刻,恍惚间忽然发觉,人在健康的时候有各种各样的精神追求,而只有在生病时,才知道拥有健康的身体,才是我们重中之重的毕生追求。大多数的项目都在上午检查完了,只有一项颅脑CT扫描需要等到下</div> </li> <li><a href="/article/1759459990688575488.htm" title="或空灵,或高雅,或风情,都是魅力女声" target="_blank">或空灵,或高雅,或风情,都是魅力女声</a> <span class="text-muted">岛上的安迪</span> <div>朋友最近在看乘风破浪的姐姐,有点奇怪他们俩会喜欢这样的节目。不过大概也如她所说,看唱唱跳跳的很轻松,况且还都是美人,也是很容易开心的。我已许久没有看综艺节目。以前喜欢的极挑,自从不是原班人马之后,就弃了。后来也慢慢习惯了不看电视节目,只在视频网站上挑些感兴趣的电影之类的来看。但以前积下来的好电影都看得七七八八了,新出的难遇令人惊艳之作,慢慢地也就不再寻找了。偶遇一神作,就当是缘份了。今天听听魅力女</div> </li> <li><a href="/article/1757959713594556416.htm" title="因为爱,所以爱" target="_blank">因为爱,所以爱</a> <span class="text-muted">山浅4313</span> <div>关于曾厝垵的4928,它是一家涂丫馆。初识的印象,它有些神秘,也有些有趣,让人想一探究竟的感觉。那天我们仨兜兜转转来到4928门口,因为有四个小哥哥在杯子上画人像,气氛很安静,技术很精湛,惟妙惟肖的,我们仨都深深地被吸引了。便忍不住的好奇问道,这个咋收费,咋能画上去的,颜色好像看起来很难上色。七七八八问了一通,半天也没得四个小哥哥回应。一会后一个小哥哥似笑非笑的给了我一张名片,上面写上,如需了解,</div> </li> <li><a href="/article/1757731453078093824.htm" title="春天的雪" target="_blank">春天的雪</a> <span class="text-muted">dxalzt</span> <div>近几天的天气预报都说要下雪,还煞有介事地说,要把一冬天欠的雪都还上。我也很盼着下雪,下透一场大雪,春天才能正式上岗。今天下午果然阴天了,雪花也飘飘洒洒地从天空中落下,我心里还默念着“天气预报诚不欺我”。这会儿夜深人静,来到窗前想欣赏白雪覆盖的美景,看到的情景却是这样的。图片发自App雪不知何时已经停了。地上的雪也已经化得七七八八了。看来,该冬天下的雪还得在冬天下完,留到春天找补也是枉然。时令不等人</div> </li> <li><a href="/article/1757632793195593728.htm" title="呼和浩特新手房东的装修之路(1)-买房验房篇" target="_blank">呼和浩特新手房东的装修之路(1)-买房验房篇</a> <span class="text-muted">君子不成器</span> <div>首先是作者简介,内蒙人,在呼市读书,在呼市工作,工作一年多攒下一万多,加父母赞助的钱,加公积金贷款,在呼市买了属于自己的房子。由于父母不在呼市,所以说,整个房子是我自己设计,自己找工人装修的(网上有那种房子交给父母装修的图片,简直不忍直视)。最主要是,到现在为止没有发现呼和浩特装修的一个指南或者参考,现在装的七七八八了,感觉有得意的地方,也有不满的地方,踩过的坑(主要讲踩过的坑),捡过的便宜(基本</div> </li> <li><a href="/article/1757414599277166592.htm" title="224 恢复日常写作状态,先找下手感" target="_blank">224 恢复日常写作状态,先找下手感</a> <span class="text-muted">云烟</span> <div>家已经彻底搬完,房间也收拾的七七八八了,不用再从早忙到晚,我的写作计划又开始重新上路了。昨天由于太累,只好草草地写上了几百字,目的是为了保持手感,而不是为了不断更。最初开始持续写作的时候,总觉得断更一天节奏就彻底乱了,抱着这个想法仅因写完忘发表就导致自己日更失败。但昨天并没有这种感觉。严格来说,昨天和前天都是随便写了点,并没有深入思考些什么,甚至可以当做吐槽。虽然没有断了日更的节奏,但我发现今天再</div> </li> <li><a href="/article/1757391555406086144.htm" title="清单|独处时做的事儿,让我们感知自得其乐的快乐" target="_blank">清单|独处时做的事儿,让我们感知自得其乐的快乐</a> <span class="text-muted">少女情怀总是诗诗诗</span> <div>七夕节生日那天,从一大早开始就收到各种各样的祝福,一直到晚上,也有许多消息发来。一瞬间,仿佛沉浸在幸福的海洋里,要开心得冒泡泡,可是一天下来,要回复好些消息,其实也是有让我感到压力的,我把它称之为甜蜜的负担。直到第二天早起之后,还有些许消息要回复,刹那间就觉着有些头大,我决定,让自己从中抽离出来,关掉网络,与自己相处,一上午的时光过去,能量恢复得七七八八了。这种感觉真的很神奇,哪怕是自顾自地做一会</div> </li> <li><a href="/article/1757201423767977984.htm" title="python从入门到精通pdf下载,python3.8从入门到精通pdf" target="_blank">python从入门到精通pdf下载,python3.8从入门到精通pdf</a> <span class="text-muted">w12130826</span> <a class="tag" taget="_blank" href="/search/pygame/1.htm">pygame</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a> <div>本篇文章给大家谈谈python3.8从入门到精通pdf百度云下载,以及python从入门到精通电子书下载,希望对各位有所帮助,不要忘了收藏本站喔。从0开始学Python,就问你一句:慌不慌?说句实在的,慌,可能是因为你自己没有完整的规划,其实就是不知道从何下手,七七八八乱学一通自然还是觉得无厘头。但今天,我要跟你讲,莫慌!恰好前几天从朋友那得到一套最强“Python编程三剑客”:《Python编程</div> </li> <li><a href="/article/1757026990130479104.htm" title="从屏幕改变命运文想到的一些七七八八" target="_blank">从屏幕改变命运文想到的一些七七八八</a> <span class="text-muted">LJXue</span> <div>前几天一篇关于远程教育的文章刷屏了,讲的是边远地区的学校通过远程连线的方式听比较发达地区的课堂的课。文章的基调是正面的,让人觉得这是一个公益性的事情在做。然而作为一个在中国长大的小孩,我的第一反应是,这样的远程资源应该不是所有人都能享受到的。再结合这个年代凡事等几天再看真相的原则,坐等了一下别人去挖一挖背后的利益体(详见后面的链接)。从几篇文章里面可以看出,这样的远程教育还是能让一些边远地区的学生</div> </li> <li><a href="/article/1756898676006993920.htm" title="抗争" target="_blank">抗争</a> <span class="text-muted">思慕一生</span> <div>总是深呼吸几次要换好几种理由来安慰自己想要把胸怀再撑大一点点可以容纳所有的委屈这一生,总是逃不掉七七八八的不如意所有的一地鸡毛不会自己消失何必欺骗自己去佛系变成了河东狮子又如何那也是不曾认输的抗争image.png</div> </li> <li><a href="/article/1756855344648175616.htm" title="冬天就该待在温暖的地方,穿短袖,开风扇!" target="_blank">冬天就该待在温暖的地方,穿短袖,开风扇!</a> <span class="text-muted">随之森淼</span> <div>今年春节非常难得有七天的假期,早早安排了去上海的行程。为什么把地点定在上海呢?当时就是把天气地图拿出来,沿着0°线一瞅,以北太冷的地方全部放弃,就在以南的地方选。可南方大多数地方都走得七七八八,有些大热门的景点,脚趾头想都觉得春节游客会特别多。最后一拍脑袋定了上海,到国际大都市里过个年。大年初二的夜班机之前,把所有亲戚、长辈溜了个遍,七天的任务挤在了两天内完成,马不停蹄地赶场换场,终于在无数次红包</div> </li> <li><a href="/article/1756851647352422400.htm" title="2020-08-21咆哮帝队长和冷淡女法医?除了这两点这部剧你还能记住什么" target="_blank">2020-08-21咆哮帝队长和冷淡女法医?除了这两点这部剧你还能记住什么</a> <span class="text-muted">王家王侯</span> <div>大家都知道我最近的日常就是看电视剧,而对我于我来说每天必然会看的一点就是刑侦类,然后搜到了《骨语》。其实这部电视剧我去年就已经看过了,但是毕竟忘得七七八八了,而且记得不难看,又算是个新电视剧,所以就决定重新看一次。毕竟只要剧好看,重新看只会收获新的感悟。我是一个多年的侦探谜,基本上市面上所有刑侦类案件的手法什么对于我来说都已经是可以基本勘破的的程度了,所以和我男朋友一起看剧的时候,为了方便他了解剧</div> </li> <li><a href="/article/1756654871089528832.htm" title="收破烂可能是遗传吧" target="_blank">收破烂可能是遗传吧</a> <span class="text-muted">EEEElena</span> <div>周末天气不佳,窝在家里收拾房间,收出了一堆旧衣服、旧书、包装袋、各种七七八八留着的“垃圾”。想到以前总说外婆,这舍不得吃那舍不得穿,什么都留着,留到最后都坏了霉了。还有我妈,总喜欢囤货,各种买,买完就收起来,收到了看不见的地方,然后忘记存在。哦,原来这真是遗传,我还有一柜子学生时代的教科书、还有一箱子几年没再穿过的衣服,以及各种当年觉得以后会有用有纪念价值的“垃圾”,其实十年都没有再拿出来过,白白</div> </li> <li><a href="/article/1756573263766503424.htm" title="日常随笔" target="_blank">日常随笔</a> <span class="text-muted">G草雨田</span> <div>最近的常态就是改卷子改卷子,讲评作业纸,错的着重讲。放学了,继续改孩子们在校考的,改完统计完分析完已经九点多,七七八八收拾回家就差不多睡觉了。其实做来做去知识点就是在那里,但是为什么就有的同学要错呢?一是没仔细看题,自以为是做了,二还是细节问题,逗号句号标点错字等。但是对于孩子们来说没有错别字也不是很可能。所以做了又做就是为了查漏补缺,不要再犯过错,不要错的要严厉批评,不懂的那就好好教。他们懂为止</div> </li> <li><a href="/article/1756427214322155520.htm" title="观后感" target="_blank">观后感</a> <span class="text-muted">深信_23号李娇妍</span> <div>图片发自App相比较起电视剧其实更钟爱电影不会过于冗长看的人不耐烦但也有个小缺点两个小时的电影可能没办法更好的把人物情感展现得特别好但是韩剧却是个意外。这部同学已经刷了四遍在看第五遍的剧的的确确就是这么吸引人。图片发自App高中时看过一遍现在剧情已经忘的七七八八的了重温这部剧带给我的感受是新的。看到第六第七集称霸全国的棋王崔泽在外受万人瞩目尽管只有18却比当大官还要受人尊重可回到胡同回到一起长大的</div> </li> <li><a href="/article/1756396652958072832.htm" title="挑战1000天演讲打卡的第117天" target="_blank">挑战1000天演讲打卡的第117天</a> <span class="text-muted">蝶尚彩欣</span> <div>大家好,我是王彩欣,今天是2019年10月25日,我挑战1000天演讲打卡的第117天。今天想跟大家分享的主题是我为什么要学习女性成长智慧,学习完的收获是什么?在学习以前我也是内心缺乏力量的,虽然也在学七七八八的东西,但是根本不知道如何将它落地,每次参加各种活动,也没办法表达清楚我是做什么的。通过学习公众演说,我发现我可以用最简短的语言清晰的表达清楚我是做什么的,我能给在场所有的人带来什么样的价值</div> </li> <li><a href="/article/1756391288334204928.htm" title="#Dairy168 忙忙碌碌" target="_blank">#Dairy168 忙忙碌碌</a> <span class="text-muted">夏日de雨倪</span> <div>2021-04-19周一晴上午学习了一篇德语文章,希望能坚持下去。七七八八弄完以后也到返点了,做了煎饺。烤了肉。味道不错。蔬菜有点烤焦了晚上,DJ做了石锅拌饭。还不错。下午,一节录屏课,一节在线课。晚上把一节课补完做好笔记。还有练习没有做完。还有一门课没补。明天八点就有课,几乎一天的安排。日式煎饺。加油吧!(ಡωಡ)晚安~于布伦瑞克</div> </li> <li><a href="/article/1756376497259560960.htm" title="其实,每天都可以像今天一样,是美好的一天" target="_blank">其实,每天都可以像今天一样,是美好的一天</a> <span class="text-muted">点亮人生</span> <div>今天,是美好的一天。图片发自App虽然,七七八八的事情忙下来,又晚睡了,但却因为充分地感觉到——今天是美好的一天,这份美好感觉所营造的幸福感在心头温润开来,却不会为晚睡而焦虑了。最近自己在修炼让自己真正安好起来,修炼放下,修炼别有过头的执念,修炼虽仍见孩子或爱人不耐烦而自己依旧要耐心下来,修炼真心的珍惜,珍惜拥有,珍惜当下。那日又一次听闻一个身边朋友同时经历癌痛与突发失去亲人两桩不幸的消息,自己也</div> </li> <li><a href="/article/1756339182696939520.htm" title="生在“注意力四分五裂”的时代,你必须得学会自救" target="_blank">生在“注意力四分五裂”的时代,你必须得学会自救</a> <span class="text-muted">文然清欢</span> <div>生活中,你是不是也经常这样:早上起来,先刷手机,该做的事情延迟做,时间在不经意间溜走想做一件事,却总是被七七八八的消息“勾走”注意力注意力集中时间短暂,不到五分钟,就想开小差你不用焦虑,也不用怀疑自我能力出了问题。你只是患上了当下最流行的“注意力涣散症”。也就是说,你的注意力总是被分散。例如,你想打开手机查个文档,却不自觉地打开了微信消息,刷起了朋友圈或者打开了网页,看起了热点新闻。过了一会才回过</div> </li> <li><a href="/article/1756185834979082240.htm" title="给未来的你" target="_blank">给未来的你</a> <span class="text-muted">草木余人</span> <div>今天突发奇想地想给未来的你写一封信。可能是居安思危,也可能是胡思乱想,反正就是想给你写一封信,带着最坏的也是最好的祝福写下这些文字,希望你认真看完。终究人是大概率是不可能一帆风顺的,我们可能会遇见一些难受的事,一些七七八八的坎,一些甚至你当下觉得过不去的坎,但是这些都只是自己觉得,换一种思路就是海阔天空。我希望你无论遇到什么,都勇敢地走下去,不要去想一些乱七八糟的,终结自己的生命是最懦夫的行为,也</div> </li> <li><a href="/article/9.htm" title="apache ftpserver-CentOS config" target="_blank">apache ftpserver-CentOS config</a> <span class="text-muted">gengzg</span> <a class="tag" taget="_blank" href="/search/apache/1.htm">apache</a> <div><server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://mina.apache.o</div> </li> <li><a href="/article/136.htm" title="优化MySQL数据库性能的八种方法" target="_blank">优化MySQL数据库性能的八种方法</a> <span class="text-muted">AILIKES</span> <a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a> <div>1、选取最适用的字段属性   MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快。因此,在创建表的时候,为了获得更好的 性能,我们可以将表中字段的宽度设得尽可能小。例如,在定义邮政编码这个字段时,如果将其设置为CHAR(255),显然给数据库增加了不必要的空间,甚至使用VARCHAR这种类型也是多余的,因为CHAR(6)就可以很</div> </li> <li><a href="/article/263.htm" title="JeeSite 企业信息化快速开发平台" target="_blank">JeeSite 企业信息化快速开发平台</a> <span class="text-muted">Kai_Ge</span> <a class="tag" taget="_blank" href="/search/JeeSite/1.htm">JeeSite</a> <div>JeeSite 企业信息化快速开发平台 平台简介 JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的开源Java EE快速开发平台。 JeeSite本身是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓存,Activit为工作流</div> </li> <li><a href="/article/390.htm" title="通过Spring Mail Api发送邮件" target="_blank">通过Spring Mail Api发送邮件</a> <span class="text-muted">120153216</span> <a class="tag" taget="_blank" href="/search/%E9%82%AE%E4%BB%B6/1.htm">邮件</a><a class="tag" taget="_blank" href="/search/main/1.htm">main</a> <div>原文地址:http://www.open-open.com/lib/view/open1346857871615.html 使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章。 【Spring Mail API】 Spring Mail API都在org.spri</div> </li> <li><a href="/article/517.htm" title="Pysvn 程序员使用指南" target="_blank">Pysvn 程序员使用指南</a> <span class="text-muted">2002wmj</span> <a class="tag" taget="_blank" href="/search/SVN/1.htm">SVN</a> <div>源文件:http://ju.outofmemory.cn/entry/35762 这是一篇关于pysvn模块的指南. 完整和详细的API请参考 http://pysvn.tigris.org/docs/pysvn_prog_ref.html. pysvn是操作Subversion版本控制的Python接口模块. 这个API接口可以管理一个工作副本, 查询档案库, 和同步两个. 该</div> </li> <li><a href="/article/644.htm" title="在SQLSERVER中查找被阻塞和正在被阻塞的SQL" target="_blank">在SQLSERVER中查找被阻塞和正在被阻塞的SQL</a> <span class="text-muted">357029540</span> <a class="tag" taget="_blank" href="/search/SQL+Server/1.htm">SQL Server</a> <div>SELECT  R.session_id AS BlockedSessionID ,          S.session_id AS BlockingSessionID ,          Q1.text AS Block</div> </li> <li><a href="/article/771.htm" title="Intent 常用的用法备忘" target="_blank">Intent 常用的用法备忘</a> <span class="text-muted">7454103</span> <a class="tag" taget="_blank" href="/search/.net/1.htm">.net</a><a class="tag" taget="_blank" href="/search/android/1.htm">android</a><a class="tag" taget="_blank" href="/search/Google/1.htm">Google</a><a class="tag" taget="_blank" href="/search/Blog/1.htm">Blog</a><a class="tag" taget="_blank" href="/search/F%23/1.htm">F#</a> <div>Intent     应该算是Android中特有的东西。你可以在Intent中指定程序 要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料 。都指定好后,只要调用startActivity(),Android系统 会自动寻找最符合你指定要求的应用 程序,并执行该程序。 下面列出几种Intent 的用法 显示网页: </div> </li> <li><a href="/article/898.htm" title="Spring定时器时间配置" target="_blank">Spring定时器时间配置</a> <span class="text-muted">adminjun</span> <a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/%E6%97%B6%E9%97%B4%E9%85%8D%E7%BD%AE/1.htm">时间配置</a><a class="tag" taget="_blank" href="/search/%E5%AE%9A%E6%97%B6%E5%99%A8/1.htm">定时器</a> <div>红圈中的值由6个数字组成,中间用空格分隔。第一个数字表示定时任务执行时间的秒,第二个数字表示分钟,第三个数字表示小时,后面三个数字表示日,月,年,< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> 测试的时候,由于是每天定时执行,所以后面三个数</div> </li> <li><a href="/article/1025.htm" title="POJ 2421 Constructing Roads 最小生成树" target="_blank">POJ 2421 Constructing Roads 最小生成树</a> <span class="text-muted">aijuans</span> <a class="tag" taget="_blank" href="/search/%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/1.htm">最小生成树</a> <div>来源:http://poj.org/problem?id=2421 题意:还是给你n个点,然后求最小生成树。特殊之处在于有一些点之间已经连上了边。 思路:对于已经有边的点,特殊标记一下,加边的时候把这些边的权值赋值为0即可。这样就可以既保证这些边一定存在,又保证了所求的结果正确。 代码: #include <iostream> #include <cstdio></div> </li> <li><a href="/article/1152.htm" title="重构笔记——提取方法(Extract Method)" target="_blank">重构笔记——提取方法(Extract Method)</a> <span class="text-muted">ayaoxinchao</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E9%87%8D%E6%9E%84/1.htm">重构</a><a class="tag" taget="_blank" href="/search/%E6%8F%90%E7%82%BC%E5%87%BD%E6%95%B0/1.htm">提炼函数</a><a class="tag" taget="_blank" href="/search/%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F/1.htm">局部变量</a><a class="tag" taget="_blank" href="/search/%E6%8F%90%E5%8F%96%E6%96%B9%E6%B3%95/1.htm">提取方法</a> <div>提取方法(Extract Method)是最常用的重构手法之一。当看到一个方法过长或者方法很难让人理解其意图的时候,这时候就可以用提取方法这种重构手法。   下面是我学习这个重构手法的笔记:   提取方法看起来好像仅仅是将被提取方法中的一段代码,放到目标方法中。其实,当方法足够复杂的时候,提取方法也会变得复杂。当然,如果提取方法这种重构手法无法进行时,就可能需要选择其他</div> </li> <li><a href="/article/1279.htm" title="为UILabel添加点击事件" target="_blank">为UILabel添加点击事件</a> <span class="text-muted">bewithme</span> <a class="tag" taget="_blank" href="/search/UILabel/1.htm">UILabel</a> <div>    默认情况下UILabel是不支持点击事件的,网上查了查居然没有一个是完整的答案,现在我提供一个完整的代码。   UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, listV.frame.size.width - 60, listV.frame.size.height)]</div> </li> <li><a href="/article/1406.htm" title="NoSQL数据库之Redis数据库管理(PHP-REDIS实例)" target="_blank">NoSQL数据库之Redis数据库管理(PHP-REDIS实例)</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/redis/1.htm">redis</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/NoSQL/1.htm">NoSQL</a> <div>一.redis.php <?php //实例化 $redis = new Redis(); //连接服务器 $redis->connect("localhost"); //授权 $redis->auth("lamplijie"); //相关操</div> </li> <li><a href="/article/1533.htm" title="SecureCRT使用备注" target="_blank">SecureCRT使用备注</a> <span class="text-muted">bingyingao</span> <a class="tag" taget="_blank" href="/search/secureCRT/1.htm">secureCRT</a><a class="tag" taget="_blank" href="/search/%E6%AF%8F%E9%A1%B5/1.htm">每页</a><a class="tag" taget="_blank" href="/search/%E8%A1%8C%E6%95%B0/1.htm">行数</a> <div>SecureCRT日志和卷屏行数设置 一、使用securecrt时,设置自动日志记录功能。 1、在C:\Program Files\SecureCRT\下新建一个文件夹(也就是你的CRT可执行文件的路径),命名为Logs; 2、点击Options -> Global Options -> Default Session -> Edite Default Sett</div> </li> <li><a href="/article/1660.htm" title="【Scala九】Scala核心三:泛型" target="_blank">【Scala九】Scala核心三:泛型</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/scala/1.htm">scala</a> <div>泛型类 package spark.examples.scala.generics class GenericClass[K, V](val k: K, val v: V) { def print() { println(k + "," + v) } } object GenericClass { def main(args: Arr</div> </li> <li><a href="/article/1787.htm" title="素数与音乐" target="_blank">素数与音乐</a> <span class="text-muted">bookjovi</span> <a class="tag" taget="_blank" href="/search/%E7%B4%A0%E6%95%B0/1.htm">素数</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E5%AD%A6/1.htm">数学</a><a class="tag" taget="_blank" href="/search/haskell/1.htm">haskell</a> <div>    由于一直在看haskell,不可避免的接触到了很多数学知识,其中数论最多,如素数,斐波那契数列等,很多在学生时代无法理解的数学现在似乎也能领悟到那么一点。     闲暇之余,从图书馆找了<<The music of primes>>和<<世界数学通史>>读了几遍。其中素数的音乐这本书与软件界熟知的&l</div> </li> <li><a href="/article/1914.htm" title="Java-Collections Framework学习与总结-IdentityHashMap" target="_blank">Java-Collections Framework学习与总结-IdentityHashMap</a> <span class="text-muted">BrokenDreams</span> <a class="tag" taget="_blank" href="/search/Collections/1.htm">Collections</a> <div>        这篇总结一下java.util.IdentityHashMap。从类名上可以猜到,这个类本质应该还是一个散列表,只是前面有Identity修饰,是一种特殊的HashMap。         简单的说,IdentityHashMap和HashM</div> </li> <li><a href="/article/2041.htm" title="读《研磨设计模式》-代码笔记-享元模式-Flyweight" target="_blank">读《研磨设计模式》-代码笔记-享元模式-Flyweight</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/1.htm">设计模式</a> <div>声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java</div> </li> <li><a href="/article/2168.htm" title="PS人像润饰&调色教程集锦" target="_blank">PS人像润饰&调色教程集锦</a> <span class="text-muted">cherishLC</span> <a class="tag" taget="_blank" href="/search/PS/1.htm">PS</a> <div>  1、仿制图章沿轮廓润饰——柔化图像,凸显轮廓 http://www.howzhi.com/course/retouching/   新建一个透明图层,使用仿制图章不断Alt+鼠标左键选点,设置透明度为21%,大小为修饰区域的1/3左右(比如胳膊宽度的1/3),再沿纹理方向(比如胳膊方向)进行修饰。   所有修饰完成后,对该润饰图层添加噪声,噪声大小应该和</div> </li> <li><a href="/article/2295.htm" title="更新多个字段的UPDATE语句" target="_blank">更新多个字段的UPDATE语句</a> <span class="text-muted">crabdave</span> <a class="tag" taget="_blank" href="/search/update/1.htm">update</a> <div>更新多个字段的UPDATE语句                    update tableA a set (a.v1, a.v2, a.v3, a.v4) = --使用括号确定更新的字段范围 </div> </li> <li><a href="/article/2422.htm" title="hive实例讲解实现in和not in子句" target="_blank">hive实例讲解实现in和not in子句</a> <span class="text-muted">daizj</span> <a class="tag" taget="_blank" href="/search/hive/1.htm">hive</a><a class="tag" taget="_blank" href="/search/not+in/1.htm">not in</a><a class="tag" taget="_blank" href="/search/in/1.htm">in</a> <div>本文转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/03/2842855.html 当前hive不支持 in或not in 中包含查询子句的语法,所以只能通过left join实现。 假设有一个登陆表login(当天登陆记录,只有一个uid),和一个用户注册表regusers(当天注册用户,字段只有一个uid),这两个表都包含</div> </li> <li><a href="/article/2549.htm" title="一道24点的10+种非人类解法(2,3,10,10)" target="_blank">一道24点的10+种非人类解法(2,3,10,10)</a> <span class="text-muted">dsjt</span> <a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a> <div>这是人类算24点的方法?!!! 事件缘由:今天晚上突然看到一条24点状态,当时惊为天人,这NM叫人啊?以下是那条状态 朱明西 : 24点,算2 3 10 10,我LX炮狗等面对四张牌痛不欲生,结果跑跑同学扫了一眼说,算出来了,2的10次方减10的3次方。。我草这是人类的算24点啊。。 然后么。。。我就在深夜很得瑟的问室友求室友算 刚出完题,文哥的暴走之旅开始了 5秒后</div> </li> <li><a href="/article/2676.htm" title="关于YII的菜单插件 CMenu和面包末breadcrumbs路径管理插件的一些使用问题" target="_blank">关于YII的菜单插件 CMenu和面包末breadcrumbs路径管理插件的一些使用问题</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/yii/1.htm">yii</a><a class="tag" taget="_blank" href="/search/framework/1.htm">framework</a> <div>在使用 YIi的路径管理工具时,发现了一个问题。                    <?php         </div> </li> <li><a href="/article/2803.htm" title="对象与关系之间的矛盾:“阻抗失配”效应[转]" target="_blank">对象与关系之间的矛盾:“阻抗失配”效应[转]</a> <span class="text-muted">come_for_dream</span> <a class="tag" taget="_blank" href="/search/%E5%AF%B9%E8%B1%A1/1.htm">对象</a> <div>概述   “阻抗失配”这一词组通常用来描述面向对象应用向传统的关系数据库(RDBMS)存放数据时所遇到的数据表述不一致问题。C++程序员已经被这个问题困扰了好多年,而现在的Java程序员和其它面向对象开发人员也对这个问题深感头痛。   “阻抗失配”产生的原因是因为对象模型与关系模型之间缺乏固有的亲合力。“阻抗失配”所带来的问题包括:类的层次关系必须绑定为关系模式(将对象</div> </li> <li><a href="/article/2930.htm" title="学习编程那点事" target="_blank">学习编程那点事</a> <span class="text-muted">gcq511120594</span> <a class="tag" taget="_blank" href="/search/%E7%BC%96%E7%A8%8B/1.htm">编程</a><a class="tag" taget="_blank" href="/search/%E4%BA%92%E8%81%94%E7%BD%91/1.htm">互联网</a> <div>一年前的夏天,我还在纠结要不要改行,要不要去学php?能学到真本事吗?改行能成功吗?太多的问题,我终于不顾一切,下定决心,辞去了工作,来到传说中的帝都。老师给的乘车方式还算有效,很顺利的就到了学校,赶巧了,正好学校搬到了新校区。先安顿了下来,过了个轻松的周末,第一次到帝都,逛逛吧! 接下来的周一,是我噩梦的开始,学习内容对我这个零基础的人来说,除了勉强完成老师布置的作业外,我已经没有时间和精力去</div> </li> <li><a href="/article/3057.htm" title="Reverse Linked List II" target="_blank">Reverse Linked List II</a> <span class="text-muted">hcx2013</span> <a class="tag" taget="_blank" href="/search/list/1.htm">list</a> <div>Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return </div> </li> <li><a href="/article/3184.htm" title="Spring4.1新特性——页面自动化测试框架Spring MVC Test HtmlUnit简介" target="_blank">Spring4.1新特性——页面自动化测试框架Spring MVC Test HtmlUnit简介</a> <span class="text-muted">jinnianshilongnian</span> <a class="tag" taget="_blank" href="/search/spring+4.1/1.htm">spring 4.1</a> <div>目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异常处理 Spring4.1新特性——数据库集成测试脚本初始化 Spring4.1新特性——Spring MVC增强 Spring4.1新特性——页面自动化测试框架Spring MVC T</div> </li> <li><a href="/article/3311.htm" title="Hadoop集群工具distcp" target="_blank">Hadoop集群工具distcp</a> <span class="text-muted">liyonghui160com</span> <div>    1. 环境描述 两个集群:rock 和 stone rock无kerberos权限认证,stone有要求认证。 1. 从rock复制到stone,采用hdfs Hadoop distcp -i hdfs://rock-nn:8020/user/cxz/input hdfs://stone-nn:8020/user/cxz/运行在rock端,即源端问题:报版本</div> </li> <li><a href="/article/3438.htm" title="一个备份MySQL数据库的简单Shell脚本" target="_blank">一个备份MySQL数据库的简单Shell脚本</a> <span class="text-muted">pda158</span> <a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/%E8%84%9A%E6%9C%AC/1.htm">脚本</a> <div>  主脚本(用于备份mysql数据库):   该Shell脚本可以自动备份 数据库。只要复制粘贴本脚本到文本编辑器中,输入数据库用户名、密码以及数据库名即可。我备份数据库使用的是mysqlump 命令。后面会对每行脚本命令进行说明。    1. 分别建立目录“backup”和“oldbackup”   #mkdir /backup   #mkdir /oldbackup  </div> </li> <li><a href="/article/3565.htm" title="300个涵盖IT各方面的免费资源(中)——设计与编码篇" target="_blank">300个涵盖IT各方面的免费资源(中)——设计与编码篇</a> <span class="text-muted">shoothao</span> <a class="tag" taget="_blank" href="/search/IT%E8%B5%84%E6%BA%90/1.htm">IT资源</a><a class="tag" taget="_blank" href="/search/%E5%9B%BE%E6%A0%87%E5%BA%93/1.htm">图标库</a><a class="tag" taget="_blank" href="/search/%E5%9B%BE%E7%89%87%E5%BA%93/1.htm">图片库</a><a class="tag" taget="_blank" href="/search/%E8%89%B2%E5%BD%A9%E6%9D%BF/1.htm">色彩板</a><a class="tag" taget="_blank" href="/search/%E5%AD%97%E4%BD%93/1.htm">字体</a> <div> A. 免费的设计资源 Freebbble:来自于Dribbble的免费的高质量作品。 Dribbble:Dribbble上“免费”的搜索结果——这是巨大的宝藏。 Graphic Burger:每个像素点都做得很细的绝佳的设计资源。 Pixel Buddha:免费和优质资源的专业社区。 Premium Pixels:为那些有创意的人提供免费的素材。 </div> </li> <li><a href="/article/3692.htm" title="thrift总结 - 跨语言服务开发" target="_blank">thrift总结 - 跨语言服务开发</a> <span class="text-muted">uule</span> <a class="tag" taget="_blank" href="/search/thrift/1.htm">thrift</a> <div>官网 官网JAVA例子 thrift入门介绍 IBM-Apache Thrift - 可伸缩的跨语言服务开发框架 Thrift入门及Java实例演示 thrift的使用介绍   RPC    POM: <dependency> <groupId>org.apache.thrift</groupId> </div> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong>按字母分类:</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/">首页</a> - <a target="_blank" href="/custom/about.htm">关于我们</a> - <a target="_blank" href="/search/Java/1.htm">站内搜索</a> - <a target="_blank" href="/sitemap.txt">Sitemap</a> - <a target="_blank" href="/custom/delete.htm">侵权投诉</a> </div> <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved. <!-- <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>--> </div> </div> </footer> <!-- 代码高亮 --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html>