Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍 本节,简单的介绍EF中的ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager这个几个比较重要的类,它们都位于System.Data.Entity
Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍
本节,简单的介绍EF中的ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager这个几个比较重要的类,它们都位于System.Data.Entity.dll下的System.Data.Objects命名空间下。在后续的章节中,我们经常会用到它们的某些方法,以便完成我们的某些操作或目的。本节,简单的说明一下以后我们可能会用到的各个类的方法,以方便我们后续的学习。
ObjectContext封装 .NET Framework 和数据库之间的连接。此类用作“创建”、“读取”、“更新”和“删除”操作的网关。
ObjectContext类为主类,用于与作为对象(这些对象为EDM 中定义的实体类型的实例)的数据进行交互。
ObjectContext类的实例封装以下内容:
l 到数据库的连接,以EntityConnection 对象的形式封装。
l 描述该模型的元数据,以MetadataWorkspace 对象的形式封装。
l 用于管理缓存中持久保存的对象的ObjectStateManager 对象。
ObjectContext类的成员方法以说明如下所示:
l AcceptAllChanges()
接受所有对该实体对象的更改
l AddObject(string,object)
将实体对象添加到制定的实体容器中
l ApplyPropertyChanges(string,object)
将以指派的实体对象属性的更改应用到容器中对应的原对象。
l Attach(System.Data.Objects.DataClasses.IEntityWithKey entity)
将带主键的实体对象附加到默认的容器中
l Attach(string,object)
将实体对象附加到指定的实体容器中
l CreateEntityKey(string,object)
给指定的实体对象创建实体主键或如果已存在实体主键,则直接返回该实体的主键
l CreateQuery(string,params ObjectParameter[])
从给定的查询字符串创建ObjectQuery对象。
l DeleteObject(object)
删除指定的实体对象
l Detach(object)
移除指定的实体对象
l ExecuteFunction(string,params ObjectParameter[])
对默认容器执行给定的函数。
l GetObjectByKey(System.Data.EntityKey key)
通过主键KEY从 ObjectStateManager 中检索对象(如果存在);否则从存储区中检索。
l Refresh(System.Data.Objects.RefreshMode refreshMode, object entity)
按指定持久更新模式,使用指定实体的存储区数据更新ObjectStateManager。。
l Refresh(System.Data.Objects.RefreshMode refreshMode, System.Collections.IEnumerable collection)
按指定持久处理模式,使用指定实体集的存储区数据更新ObjectStateManager。
l SaveChanges(bool )
将所有更新持久保存到存储区中。参数是客户端事务支持所需的参数。参数为true则在更新后自动将更改应用到ObjectStateManager中的实体。如果为false,则在更新后还需要调用AcceptAllChanges()以便更新ObjectStateManager中的实体。
l SaveChanges()
将所有更新持久保存到存储区中
l TryGetObjectByKey(System.Data.EntityKey,out object)
尝试从指定实体主键返回该实体
以上各个方法的具体用法,将在后面介绍。
接着,再看看有用的类ObjectQuery。
ObjectQuery有个有用的方法ToTraceString(),这个方法用于追踪所执行的SQL语句,通过此方法我们可以获取所执行的SQL语句,以便我们查看、分析具体执行的SQL语句。(类似Nhibernate配置文件中的showsql节)
再了解一下ObjectStateEntry。
ObjectStateEntry维护实体实例或关系实例的状态(已添加、已删除、已分离、已修改或未更改)、键值和原始值。还管理已修改属性的列表。其包含一下方法:
l AcceptChanges
接受当前值作为原始值,并将实体标记为Unchanged()。
l Delete
将实体标记为Deleted()。如果实体处于 Added()()() 状态,它将为Detached()。
l GetModifiedProperties
返回标记为Modified()的属性名称。
l SetModified
将状态设置为Modified()。
l SetModifiedProperty
将指定的属性标记为Modified()。
接着,再看看ObjectStateManager。
ObjectStateManager用于维护对象映射、对象状态/标识管理以及实体实例或关系实例的持久性。
l GetObjectStateEntries
获取给定EntityState的ObjectStateEntry集合。
l GetObjectStateEntry
获取给定的EntityKey 对应的ObjectStateEntry
现在,几个重要的类简单介绍完毕。后面,我们将具体学习它们的使用。
原文:http://www.shangxueba.com/jingyan/104559.html

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
