Entityframework mergeoption Application

Source: Internet
Author: User

Recently, I tried to use EF in Sharepoint to access the local database. Because SharePoint can only reference the. net3.5ProgramTherefore, you can only use the ef1.1 version :-(), let's talk about the specific problems.

First, let's take a look at mergeoption.

Objectcontext manages the returned objects in four ways:

1. appendonly -- if the object to be obtained is in the context, it is obtained directly from the context. If not, it is appended to the context. This is the default method, the advantage of this method is that you will first find the entity you want from the memory, which is faster.

2. notracking-the context does not store objects. This means that data is retrieved from the database every time.

3. overwritechanges -- if the object to be obtained is In the context, the object value will be overwritten by the database value, and then return, if not, the retrieved entity is appended to the context.

4. preservechanges -- if the object to be obtained is In the context, the attributes modified by the user in the object remain unchanged, and other attributes are overwritten by database values, if not, the retrieved entity is appended to the context.

Problems I encountered

An object is constructed in the context and marked as an added object. The flowcode field of the object is automatically generated by the database trigger.

Basicinfo binfo = new baicinfo (); // primary key
; Guid tempguid = binfo. uiid = guid. newguid ();
Binfo. discription = "test"; binfo. type = 1; CTX. basicinfoentities. Add (binfo );

Then CTX. savechanges (); The database trigger will automatically generate a flowcode field for the record, so you naturally want to obtain the flowcode of the record based on the primary key in this context,

 
String flowcode = CTX. basicinfoentities. Where (x => X. uiid = tempguid). firstordefault (). flowcode;

But the strange thing is that this flowcode cannot be obtained, but the database clearly shows that this field has been generated by the trigger. It has a value, Depressing:-(Google has found that EF will generate a large number of SQL statements to the database when savechanges () is called in the same context, and the database will be executed awkwardly, however, the trigger is executed in the database, so the database will not notify EF that the value of the flowcode field is generated using the trigger when you asked me to help you insert a piece of data, then, EF is updated to the entry corresponding to the entity of objectstatemanger. At this time, even if the binfo Object Data in the context is not the latest (relative to the database), its status is unchanged,Because the default mergeoption is appendonly, we will return the objects in the context when getting objects based on the primary key.In this way, of course, the flowcode value is not obtained.

Solution: rewrite mergeoption to notracking.

Method 1: permanently set the object set mergeoption to notracking.

 
CTX. basicinfoentities. mergeoption = mergeoption. notracking;

Method 2: set this call to notracking only.

 
String flowcode = CTX. basicinfoentities. Execute (mergeoption. notracking). Where (x => X. uiid = tempguid). firstordefault (). flowcode;

In this way, we can tell EF to fetch the data of this entity from the database, and the flowcode generated by the trigger will be taken out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.