when i try to save the the new Student object. it works fine but when i am trying to modify the data, it doesn't update the data. Instead none error is thrown on SaveChanges.
i am using code first approach (using mysql provider) and here it is the complete source.
https://www.dropbox.com/s/e34frntq8u5gsmh/SchoolManagementSystem.rar?dl=0
my tired code is this :
public ActionResult Create(Student student, HttpPostedFileBase Image)
{
try
{
if (ModelState.IsValid)
{
student = db.Students.Find(student.ID);
if (student.ID > 0)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
}
else
{
if (student.Basic == null) student.Basic = new BasicInformation();
if (Image != null && Image.ContentLength > 0)
{
student.Basic.PictureUrl = Image.FileName;
string path = Server.MapPath(("~/Images/"));
Image.SaveAs(path + Image.FileName);
}
db.Students.Add(student);
db.SaveChanges();
}
return RedirectToAction("StudentList");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(student);
}
This seems to be the modify scenario :
if (student.ID > 0)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
}
Eliminate this line of code :
db.Entry(student).State = EntityState.Modified;
and instead add code that updates the object with the new modified values.
Hope this helps.
By this line
student = db.Students.Find(student.ID);
you overwrite the student that enters the method and you lose all changes. You can do two things in stead:
Only remove the line. Your code will now attach the modified student as EntityState.Modified.
Fetch the original student from the database and copy the modified values to it:
var studentOrg = db.Students.Find(student.ID);
db.Entry(studentOrg).CurrentValues.SetValues(student);
(both SaveChanges calls can be moved to one just before return RedirectToAction...)
Option 1 will generate an update statement containing all Student's fields, but not have a roundtrip to get the original record.
Option 2 has this roundtrip, but only updates modified fields. This (option 2) can be beneficial when changes are audited or when concurrency should be minimized.
Related
I have following code. in that i am trying to update my data. but i am getting error message:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Here is my code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="CompanyId,Address,EstbalishYear,Email,IsActive")] CompanyMaster companymaster)
{
if (companymaster.CompanyId == 0)
{
return View(companymaster);
}
CompanyMaster company = db.CompanyMasters.SingleOrDefault(x => x.CompanyId == companymaster.CompanyId);
companymaster.Name = company.Name;
companymaster.InsertedBy = company.InsertedBy;
companymaster.InsertedTime = company.InsertedTime;
companymaster.UpdatedBy = 1;
companymaster.UpdatedTime = DateTime.Now;
ModelState.Remove("Name");
if (ModelState.IsValid)
{
db.Entry(companymaster).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(companymaster);
}
Please explain me how can I fix this error message?
This is because you are working with two object instances of a company master, which in reality is a single entity, with the same ID.
One (companyMaster) comes as an argument to the Edit method, via binding.
The other one (company) you are selecting from the database through db.CompanyMasters by ID
What you can do is
Select company by ID, as you do now
Set company properties from companyMaster object (vice-versa, not like you do now)
Save the company object
Please find the sample code below.
Please also note that the best practice is not to use your persistence entity model in UI layer, but rather define a DTO with a minimum set of required fields, and then map it to your entity either manually or using AutoMapper.
[HttpPost] [ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="CompanyId,Address,EstbalishYear,Email,IsActive")] CompanyMaster companymaster)
{
if (companymaster.CompanyId == 0)
{
return View(companymaster);
}
CompanyMaster company = db.CompanyMasters.SingleOrDefault(x => x.CompanyId == companymaster.CompanyId);
company.Address = companymaster.Address;
company.EstbalishYear= companymaster.EstbalishYear;
company.Email = companymaster.Email;
company.IsActive= companymaster.IsActive;
company.UpdatedBy = 1;
company.UpdatedTime = DateTime.Now;
ModelState.Remove("Name");
if (ModelState.IsValid)
{
db.SaveChanges();
return RedirectToAction("Index");
}
return View(companymaster);
}
as the question implies I want to store images into the filesystem and save a link to it in the database.
but NHibernate doesn't save the file path in the database. here is the code:
[HttpPost]
public ActionResult Edit(Item item, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
if (image != null)
{
string imageName = image.FileName;
string location = Path.Combine(Server.MapPath("~/Content/Images/ItemImages/") , imageName);
image.SaveAs(location);
item.Image= imageName;
}
menuItemRepository.SaveOrUpdate(item);
// here the debug show the image path has correctly assigned to the image property
Debug.WriteLine(item.Image);
TempData["message"] = string.Format("{0} has been saved", item.Name);
return RedirectToAction("Index", item.Parent);
}
else
{
// there is something wrong with the data values
return View(Item);
}
}
but after repositor save or update the item, when I look at the database, the image is null. I tried to assign something ele like image name and it did work but the for image path is not working!! I'm confused why this happen. does anyone have any idea?
public class Item
{
public virtual string Image { get; set; }
}
public calss ItemMap : ClassMap<Item>
{
public ItemMap()
{
Map(x => x.Image).Length(100);
}
}
//////////Repository
public T SaveOrUpdate(T entity)
{
session.SaveOrUpdate(entity);
return entity;
}
My best guess - the save is not being flushed to the database. See the documentation:
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points
from some invocations of Find() or Enumerable()
from NHibernate.ITransaction.Commit()
from ISession.Flush()
I see nothing in your code that would trigger a flush. Wrap your SaveOrUpdate in a transaction:
using (var trx = menuItemRepository.BeginTransaction())
{
menuItemRepository.SaveOrUpdate(item);
trx.Commit();
}
trx.Commit() will flush that pending update query to the database.
I had implemented a sessionPreRequest module for my MVC app. so I was doing commit() operation there.
I checked and saw my transaction is not committing and is rolling back. and checked the error and the image column in the database was nvarchar(50), but the string which had the path of image was mor than 50 characters. so I changed to nvarchar(200) and now everything works fine.
Is the essence of Project, the creation of which is necessary to check whether there is already an entity with the same name. When editing needs such as checking, but keep in mind that the old and the new name of the entity can be matched.
You also need to display an error message. For this I use interface IValidatableObject, but do not know how to tell the Validate method the object is currently being edited or created
DbContext.ValidateEntity takes the IDictionary<Object, Object> items as the second parameter. You can pass any data there and the data you pass will be passed to IValidatableObject.Validate in the ValidationContext.Items
Assuming you refer to check EF cant do for you.
This is actually difficult to check. You are checking an entity after it has been added to the context. It should not check itself and needs to consider other items in context that are not yet saved. As well as the DB. There are several 3 combinations plus an self recognition. Record a an entity record in LOCAL when ID is blank/new ie multiple new inserts needs careful coding. (Consider using temp IDs)
the not yet saved entries should be in context
Context.Set<TPoco>().Local
and get data from DB and keep in a temp list. BUT dont put in context.
Or use a SECOND context.
var matchingSet = Context.Set<TPoco>().AsNoTracking() // not into context...
.Where(t=>t.field == somevalue).ToList();
So what about logical and actual duplicates on the DB. Logical duplicates are duplicates on a field with no unique index that from a business perspective should be unique.
If you want to check those...
You need to read the DB.... BUT if these records are currently being changed, you CAN NOT just put them into the Context. You would overwrite them.
But what if the values the logical key values have changed?
Something caused a logical dup on a record on the DB may no longer be a dup once saved or vice verse. Is that still a dup or not ?
So you need to decide how you match LOCAL versus loaded records.
Ie check LOCAL and matching DB records and decidr what to do if a record is in both, only local or only db.
LOCAL ONLY and DB Only is easy.
But in both... That is your business process decision.
Problem is solved using method ModelState.AddModelError (string, string) in actions Edit and Create.
[HttpPost]
[HandleError(View="AjaxError")]
public ActionResult Edit(ProjectsViewData data)
{
if (ModelState.IsValid)
{
if (!ContainsProject(data.CurrentObject.Name))
{
db.Projects.Attach(data.CurrentObject);
db.ObjectStateManager.ChangeObjectState(data.CurrentObject, EntityState.Modified);
db.SaveChanges();
return Projects(data);
}
else
{
int projectId = (from p in db.Projects
where p.Name == data.CurrentObject.Name
select p.ProjectID).FirstOrDefault();
if (projectId == data.CurrentObject.ProjectID)
{
db.Projects.Attach(data.CurrentObject);
db.ObjectStateManager.ChangeObjectState(data.CurrentObject, EntityState.Modified);
db.SaveChanges();
return Projects(data);
}
else
{
ModelState.AddModelError("Name", Localizer.ProjectAlreadyExists);
}
}
}
data.ObjectToEdit = data.CurrentObject;
return Projects(data);
}
[HttpPost]
[HandleError(View = "AjaxError")]
public ActionResult Create(ProjectsViewData data)
{
if (ModelState.IsValid)
{
if (!ContainsProject(data.CurrentObject.Name))
{
db.Projects.AddObject(data.CurrentObject);
db.SaveChanges();
return Projects(data);
}
else
{
ModelState.AddModelError("Name", Localizer.ProjectAlreadyExists);
}
}
data.ObjectToAdd = data.CurrentObject;
return Projects(data);
}
Helper method:
private bool ContainsProject(string projectName)
{
if (projectName != null)
{
projectName = Regex.Replace(projectName.Trim(), "\\s+", " ");
List<string> projects = new List<string>();
var projectNames = (from p in db.Projects
select p.Name.Trim()).ToList();
foreach (string p in projectNames)
{
projects.Add(Regex.Replace(p, "\\s+", " "));
}
if (projects.Contains(projectName))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Been working on this C# Entity Framework update problem for a bit now. Maybe one of you can see what I'm missing.
The problem is within the userEntry portion of the code. I've traced through and made sure that userEntry is indeed populated with the information that I intend to update. When entities.SaveChanges(); is invoked, the record is not updated in the database.
int userId;
using (Entities entities = new Entities())
{
MSIFeedStoreData feedStoreEntry = entities.MSIFeedStoreDatas
.FirstOrDefault((d) => d.Alias == user.Alias);
if (Object.ReferenceEquals(feedStoreEntry, null))
{
throw new ArgumentException("The user's alias could not be located in the feed store. The user cannot be added at this time.");
}
int feedStorePersonnelIdValue;
if (Int32.TryParse(feedStoreEntry.PersonellID, out feedStorePersonnelIdValue))
{
user.EmployeeId = feedStorePersonnelIdValue;
}
else
{
throw new ApplicationException("DATABASE BUG CHECK: An entry was found in the feed store for this user but the personnel ID could not be parsed.");
}
MSIUser userEntry = entities.MSIUsers
.FirstOrDefault((u) => u.EmployeeID == feedStorePersonnelIdValue);
if (Object.ReferenceEquals(userEntry, null))
{
userEntry = Mapper.Map<MSIUser>(user);
userEntry = entities.MSIUsers.Add(userEntry);
}
else
{
Mapper.DynamicMap<User, MSIUser>(user, userEntry);
entities.MSIUsers.Attach(userEntry);
}
userId = userEntry.MSIUser_ID;
entities.SaveChanges();
}
return userId;
}
Remove the call to Attach, its already attached to the context.
Im learning MVC 4. I have created a database first project using EF5. In my edit view I want to add a product number to a customer. When I hit save I get the message below. I think it is because product number is null in the product table, hence it cannot update. Can I get around this? I have added my edit control
public ActionResult Edit(int id = 0)
{
UserProfile userprofile = db.UserProfiles.Find(id);
if (userprofile == null)
{
return HttpNotFound();
}
//ViewBag.userId = new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.UserId);THIS CREATES A NEW ENTRY IN USERPROFILE TABLE
ViewBag.Device_DeviceID = new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.Device);
ViewBag.ShippingDetails_ShippingDetailsID = new SelectList(db.ShippingDetails, "ShippingDetailsID", "Address1", userprofile.ShippingDetails_ShippingDetailsID);
return View(userprofile);
}
//
// POST: /User/Edit/5
[HttpPost]
public ActionResult Edit(UserProfile userprofile)
{
if (ModelState.IsValid)
{
db.Entry(userprofile).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
//ViewBag.userId = new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.UserId);
ViewBag.Device_DeviceID = new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.Device);
ViewBag.ShippingDetails_ShippingDetailsID = new SelectList(db.ShippingDetails, "ShippingDetailsID", "Address1", userprofile.ShippingDetails_ShippingDetailsID);
return View(userprofile);
}
"Store 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"
It looks like you dont pass Id of UserProfile from
view to controller.
You should add
#Html.HiddenFor(model => model.Id)
to your form in view
You're posting a view model, which is disconnected from your entity framework, and trying to tell the EF that it has changed -- which it doesn't know about. Try something like this instead,
var obj = yourContext.UserProfiles.Single(q=>q.Id==userProfile.Id);
obj = userprofile; // ... Map userprofile to the tracked object, obj
yourContext.SaveChanges();
Try this:
if (ModelState.IsValid)
{
db.UserProfiles.Attach(userProfile);
db.SaveChanges();
return RedirectToAction("Index");
}