Home Database Mysql Tutorial Entity Framework学习初级篇2

Entity Framework学习初级篇2

Jun 07, 2016 pm 03:49 PM
framework primary study

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


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn to completely uninstall pip and use Python more efficiently Learn to completely uninstall pip and use Python more efficiently Jan 16, 2024 am 09:01 AM

No more need for pip? Come and learn how to uninstall pip effectively! Introduction: pip is one of Python's package management tools, which can easily install, upgrade and uninstall Python packages. However, sometimes we may need to uninstall pip, perhaps because we wish to use another package management tool, or because we need to completely clear the Python environment. This article will explain how to uninstall pip efficiently and provide specific code examples. 1. How to uninstall pip The following will introduce two common methods of uninstalling pip.

A deep dive into matplotlib's colormap A deep dive into matplotlib's colormap Jan 09, 2024 pm 03:51 PM

To learn more about the matplotlib color table, you need specific code examples 1. Introduction matplotlib is a powerful Python drawing library. It provides a rich set of drawing functions and tools that can be used to create various types of charts. The colormap (colormap) is an important concept in matplotlib, which determines the color scheme of the chart. In-depth study of the matplotlib color table will help us better master the drawing functions of matplotlib and make drawings more convenient.

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

Learn the strconv.Atoi function in the Go language documentation to convert strings to integers Learn the strconv.Atoi function in the Go language documentation to convert strings to integers Nov 03, 2023 am 08:55 AM

Learn the strconv.Atoi function in the Go language documentation to convert strings to integers. The Go language is a powerful and flexible programming language. The strconv package in its standard library provides the function of string conversion. In this post, we will learn how to convert string to integer using strconv.Atoi function. First, we need to understand the purpose and declaration of the strconv.Atoi function. The description of the function in the document is as follows: funcAtoi(sstring)(i

Quickly learn pip installation and master the skills from scratch Quickly learn pip installation and master the skills from scratch Jan 16, 2024 am 10:30 AM

Learn pip installation from scratch and quickly master the skills. Specific code examples are required. Overview: pip is a Python package management tool that can easily install, upgrade and manage Python packages. For Python developers, it is very important to master the skills of using pip. This article will introduce the installation method of pip from scratch, and give some practical tips and specific code examples to help readers quickly master the use of pip. 1. Install pip Before using pip, you first need to install pip. pip

See all articles