3.c#/. NET programming common exceptions (continuous update)

Source: Internet
Author: User

1.Object reference not set to a instance of an Object.

The object is not referenced (referenced) to an instance of the object, and it is plain that there is an object that is null, but you are using it to point out the various things.

2.An entity object cannot is referenced by multiple instances of IEntityChangeTracker.

An entity object cannot be referenced by more than one IEntityChangeTracker instance. First, see the instructions in MSDN for additions and deletions in the Entity Framework, especially where you modify the object. We see a statement in the documentation that "object service uses IEntityChangeTracker instances to track changes to objects attached to ObjectContext", 1) tracker is used to track an object property change, 2) is followed by an object attached to a context, 3) The error message is that an object cannot be tracked by multiple tracker, that is, an object attached to a particular context has a tracker.

Then I think the blog to help me solve this problem is not too clear, it may be that I understand the poor ability, try to analyze it myself. Corresponds to the original post, an entity is definitely the user, why it will have multiple ientitychangetracker instances, obviously because he was attached to two context, Rolemanager.getrolebyid () method is not the same as the context of the Usermanager.add () method, which is entities.

In layman's terms, for the same entity object (Database Association) operation, do so in the same context.

3.Collection was modified; Enumeration operation may not execute.

For a foreach traversal of a collection, this error is reported if the collection is added and removed from the foreach.

This is the characteristic decision of IEnumerator itself, see the Remarks section:

Good evening--------------------------------everyone--------------------------------

The enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection (such as adding, modifying, or deleting elements), the enumerator is invalidated and unrecoverable, and the next call to MoveNext or Reset throws a InvalidOperationException. If the collection is modified between MoveNext and current, then even if the enumerator is invalid, current will return the element it is set to.

The enumerator does not have exclusive access to the collection, so enumerating through collections is inherently not a thread-safe process. Even if a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To ensure thread safety during enumeration, you can lock the collection during the entire enumeration, or catch exceptions that are thrown because of changes made by other threads.

Good evening--------------------------------everyone--------------------------------

You just need to change foreach to a for to traverse. There are a few drawing mistakes I forgot ... So much for writing today.

3.c#/. NET programming common exceptions (continuous update)

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.