zoukankan      html  css  js  c++  java
  • EntityFramework 学习 一 DbSet

    DBSet类表示一个实体的集合,用来创建、更新、删除、查询操作,DBSet<TEntity>是DBSet的泛型版本

    你可以使用DbContext获取DBSet的引用,例如dbContext.Students

    DBSet中的一些重要方法

    Method NameReturn TypeDescription
    Add Added entity type Adds the given entity to the context the Added state. When the changes are being saved, the entities in the Added states are inserted into the database. After the changes are saved, the object state changes to Unchanged.

    Example: 
    dbcontext.Students.Add(studentEntity)
    AsNoTracking<Entity> DBQuery<Entity> Returns a new query where the entities returned will not be cached in the DbContext. (Inherited from DbQuery.)

    Entities returned as AsNoTracking, will not be tracked by DBContext. This will be significant performance boost for read only entities. 


    Example: 
    var studentList = dbcontext.Students.AsNoTracking<Student>().ToList<Student>();
    Attach(Entity) Entity which was passed as parameter Attaches the given entity to the context in the Unchanged state 

    Example: 
    dbcontext.Students.Attach(studentEntity);
    Create Entity Creates a new instance of an entity for the type of this set. This instance is not added or attached to the set. The instance returned will be a proxy if the underlying context is configured to create proxies and the entity type meets the requirements for creating a proxy.

    Example: 
    var newStudentEntity = dbcontext.Students.Create();
    Find(int) Entity type Uses the primary key value to attempt to find an entity tracked by the context. If the entity is not in the context then a query will be executed and evaluated against the data in the data source, and null is returned if the entity is not found in the context or in the data source. Note that the Find also returns entities that have been added to the context but have not yet been saved to the database. 

    Example: 
    Student studEntity = dbcontext.Students.Find(1);
    Include DBQuery Returns the included non generic LINQ to Entities query against a DbContext. (Inherited from DbQuery)

    Example:
    var studentList = dbcontext.Students.Include("StudentAddress").ToList<Student>();
    var studentList = dbcontext.Students.Include(s => s.StudentAddress).ToList<Student>();
    Remove Removed entity Marks the given entity as Deleted. When the changes are saved, the entity is deleted from the database. The entity must exist in the context in some other state before this method is called.

    Example:
    dbcontext.Students.Remove(studentEntity);
    SqlQuery DBSqlQuery Creates a raw SQL query that will return entities in this set. By default, the entities returned are tracked by the context; this can be changed by calling AsNoTracking on theDbSqlQuery<TEntity> returned from this method.

    Example:
    var studentEntity = dbcontext.Students.SqlQuery("select * from student where studentid = 1").FirstOrDefault<Student>();
  • 相关阅读:
    useState 的介绍和多状态声明(二)
    PHP:相对于C#,PHP中的个性化语法
    PHP:IIS下的PHP开发环境搭建
    PHP:同一件事,有太多的方式
    Javascript:再论Javascript的单线程机制 之 DOM渲染时机
    Javascript:拦截所有AJAX调用,重点处理服务器异常
    DDD:谈谈数据模型、领域模型、视图模型和命令模型
    .NET:再论异常处理,一个真实的故事
    Javascript:由 “鸭子类型” 得出来的推论
    Workflow:采用坐标变换(移动和旋转)画箭头
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6617808.html
Copyright © 2011-2022 走看看