How can you iterate through a single model and save it multiple times so that a new record/row is created in the database?
For example I have an application form and I wish to create multiple applications using the same details but for each application I will change the applicant.
I have the following controller action to which attempts to achieve this:
public ActionResult Create([Bind(Prefix = "Application")]Application application, FormCollection collection) { List<string> selectedEmployees = new List<string>(); // Application.EmpIDs is a multiselect drop down list if (collection["Application.EmpIDs"] == null) { ModelState.AddModelError("Employee", "Please select an employee"); } else { selectedEmployees.AddRange(collection["Application.EmpIDs"].Split(',').ToList()); } if (ModelState.IsValid) { foreach (var item in selectedEmployees) { // Add emp id application.EmpID = Convert.ToInt32(item); // Add Application _repository.AddApplication(application); // Persist changes back to database _repository.Save(); } } //...more stuff }
however i end up with the following error message:
An object with the same key already exists in the ObjectStateManager. The existing object is in the Modified state. An object can only be added to the ObjectStateManager again if it is in the added state.
It sounds like the object you are looking for already exists within your context. Have you tried removing the AddApplication() call and just calling the Save() (or SaveChanges()) method on it’s own? If Entity Framework is tracking your elements,
then it will track changes (such as new or updated property values) which will be saved to the database when the Save method is called :
// Add emp id application.EmpID = Convert.ToInt32(item); // Persist changes back to database _repository.Save();
You might also want to consider taking a look at the data within your database to be sure that you don’t have any issues or duplicate keys (or any logical issues that could cause that to occur).
the AddApplication() call simply updates some additional property values. The full method is below:
public void AddApplication(Application application) { application.StatusID = 1; application.CreatedDate = DateTime.Now; application.LastModifiedDate = DateTime.Now; application.StatusDate = DateTime.Now; _entities.Applications.AddObject(application); }
there should be no issues with the database becuase each application has an autoincrement primary key.
ok i’ve managed to get this working by adding the following line after the save method:
// Detach the entity from object context _repository.Detach(application);
However I need get the ids of all the newly added applications. I have added the following list below the save method but all i get added into this list is the first application multiple times hence i end up with the same application id.
// Persist changes back to database _repository.Save();
// create list of applications added to reference new application ids // this is basically a list List<Application> applicationsSubmitted = new List<Application>(); applicationsSubmitted.Add(application);
// Detach the entity from object context
_repository.Detach(application);