Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:55:07+00:00 2026-06-04T13:55:07+00:00

I am working with Entity Framework 4 (using self-tracking entities), and accessing a view,

  • 0

I am working with Entity Framework 4 (using self-tracking entities), and accessing a view, that is the merge of two tables. So when I update the information of the view, I send the STE of the view to the repository which access to the database.

I have done the following:

When I receive my view with the updated info, I create my STE1 and STE2. How when I create an STE, this is created with an added state. So how I know for the state of the STE of the view that the state is modified, I change the state of my two STEs with the method MarkedAsModified.

Then, how I have in the STE of the view the information of the two tables, I pass the information from the view to the correct STE, and apply the changes from the STE to the objectContext.

Finally I make the saveChanges. But in this step I receive an Optimistic concurrency exception. I think that it is because The STE pass from the state to Added to Modified so the context detects that there is some modification between the creation and the SaveChanges, but also I try to AcceptChanges in the STE, later marked as modified and, apply changes and finally SaveChanges, but the problem persist.

How can I solve the problem? there is a better way to work with view and Entity Framework v4?

Thanks.
Daimroc.

EDIT 1: I still have problems. My code is the following:

Components myComponent = new Components(); //this is a STE
myComponent.Rereference = myView.Reference;
… //other properties
myComponent.MarkedAsModified(); //this is needed because I want to update information, no add a new register.
myContext.ApplyChanges(“Components”, myComponent);
miContexto.SaveChanges();

In the saveChanges, I get the exception: update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Which is the problem? can I not modified a new STE created?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:55:09+00:00Added an answer on June 4, 2026 at 1:55 pm

    I have found the way to solve the problem.

    In a first solution, the solution was to make a query to the data base to get the existing registers in the database. This add the entities in the context and then it is possible to modified the data, and save the changes correctly.

    But later, I found the way to avoid the needed to make the query the the data base to add the modified entities in the context.

    The way is using the following code:

    Customers myCustomer = new Customers  { IDCustomer = myCustomer.IDCustomer };
    myContext.Customers.Attach(myCustomer);
    
    //update the data of the entity.
    
    myContext.SaveChanges();
    

    The solution is to create a new entity setting the primary key. If the entity would have FK it would be indicated in the same way. At this point, the entity has the added state.

    Later, is attached to the context, and then can be modified. When a field is changed, the entity changes its state to modified, so when the saveChanges() is called, EF update the entity, not try to add a new one.

    I find this information in this link. In this post the solution is for deleting an entity without to retrieve it to the database, bur the idea works too if we want to modify an existing register.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a personal project using WPF with Entity Framework and Self Tracking
I want to test my Entities that are built using Entity Framework. My concern
I am using Entity Framework with the Self-Tracking Entity T4 templates, which by default
I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So
I have an MVC 3 project that I am working on using Entity Framework
I am currently working on my own version of membership using Entity Framework 4.0
Reading this MSDN article titled Working with ObjectSet (Entity Framework) It shows two examples
I am working on a simple web application made using Entity Framework 4.1 Code
I am working in ASP.Net MVC 1.0 and SQL Server 2000 using Entity Framework.
I have an application I am working on, using Entity Framework 4.1 as my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.