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 7490871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:46:51+00:00 2026-05-29T15:46:51+00:00

I built a very simple MVC3 application to do a little demo, but I’m

  • 0

I built a very simple MVC3 application to do a little demo, but I’m running against a problem; I return an entity to my view, edit it and then post it back, but in this process my entity loses it’s change-tracking capabilities. When I return the entity to my view it’s still an entity framework proxy class, but when it comes back from my view it’s a ‘Person’ class (the entity is called person).

Here’s my repository class:

public class PersonRepository : IPersonRepository
{
    public EfContext Uow { get; set; }

    public PersonRepository(IUnitOfWork uow)
    {
        Uow = uow as EfContext;
    }

    // yada yada yada

    public void Add(Person person)
    {
        Uow.Persons.Add(person);
    }
}

This entity is sent to my view that has a simple form, created with Html.EditorForModel. After that I post it back to this method:

[HttpPost]
public ActionResult Edit(Person person)
{
    if (ModelState.IsValid)
    {
        _personRepository.Add(person);
        _personRepository.Uow.Commit();

        return RedirectToAction("Index");
    }

    return View(person);
}

And tada, it’s no longer a tracked proxy class. This results in a primary key violation because entity framework is trying to add my object as a new object, where I just want entity framework to detect the changes and create an update statement instead. Oh by the way, the Commit method in the code above just calls SaveChanges(), here’s the class:

public class EfContext : DbContext, IUnitOfWork
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Person> Persons { get; set; } 

    public void Commit()
    {
        SaveChanges();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

By the way, this is my entity class:

public class Person
{
    [HiddenInput(DisplayValue = false)]
    public Guid Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public virtual ICollection<Account> Accounts { get; set; } 
}

Does anyone know how to fix this? I had this working before as far as I can remember, I just don’t know how.

Thanks in advance!

  • 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-05-29T15:46:52+00:00Added an answer on May 29, 2026 at 3:46 pm

    Define an Update method in your repository that will attach the entity and mark it as modified.

    public class PersonRepository : IPersonRepository
    {
        // yada yada yada
    
        public void Add(Person person)
        {
            Uow.Persons.Add(person);
        }
    
        public void Update(Person person)
        {
            Uow.Persons.Attach(person);
            Uow.Entry(person).State = EntityState.Modified;
        }
    }
    
    [HttpPost]
    public ActionResult Edit(Person person)
    {
        if (ModelState.IsValid)
        {
            _personRepository.Update(person);
            _personRepository.Uow.Commit();
    
            return RedirectToAction("Index");
        }
    
        return View(person);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a very simple blog application using Ruby on Rails. New to
I have built a variety of little scripts using Ruby's very simple Queue class,
This seems like a very simple problem, but I cannot get a scrollbox to
I just built a very simple add-on for Firefox, but it's only compatible with
I have built a very simple internal messaging system inside my application, i want
i built a very simple application in vb.net, installed it, it worked. then i
I've built a very simple chatroom-like ASP.NET application which displays current Online/Offline users: I
I've built a very simple table that displays 4 columns and 4 rows. When
I want to build a very simple GUI based application in linux, c++. Which
I am looking to build a very simple GUI for a C++ application. Fancyness,

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.