Answered by:
'Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.'

Question
-
User1052662409 posted
Hi All,
I am using MVC 5 with EF 6.
In my application I chose Controller with views using Entity Framwork, which automatically creates the view and methods like below.
public ActionResult Edit([Bind(Include = "bigid,vcname,vcrate_phour,dateadded,datemodi,addedby,updatedby")] OT_Band_Master oT_Band_Master) { if (ModelState.IsValid) { oT_Band_Master.datemodi = DateTime.Now.Date; oT_Band_Master.updatedby = Convert.ToInt32(Session["AdminID"].ToString()); db.Entry(oT_Band_Master).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(oT_Band_Master); }
Suppose I just guess ( not sure ) that this error comes when don't provide all fields in Edit ActionResult.
Actually I don't need to update all fields as mentioned here (bigid,vcname,vcrate_phour,dateadded,datemodi,addedby,updatedby).
I just need to only 4 fields. 2 are I am mentioning here in code.
oT_Band_Master.datemodi = DateTime.Now.Date;
oT_Band_Master.updatedby = Convert.ToInt32(Session["AdminID"].ToString());And other two it took from like below
@Html.EditorFor(model => model.vcname
Please suggest.
Thanks
Wednesday, May 22, 2019 7:20 AM
Answers
-
User1520731567 posted
Hi demoninside9,
According to your error message,This may be caused by restrictions or constraints in your model.
You could use try..catch..or check Quick Watch window in controller to look for the cause.
For example:
More details,you could refer to:
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 23, 2019 8:33 AM -
User475983607 posted
As I see the The Author_id field is required.
This is the main drawback of EF, if you want to update only few fields you have to update all fields in database table. (When you use Controller with view and entity framework).
I am looking for the solution that any fields don't have any compulsion. Fields should be free and independent for any (CRUD) operations.
You are assuming how EF works rather than taking the time to understand how it works. Anyway, the solution (programming pattern) was covered in your threads from yesterday.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 23, 2019 1:38 PM
All replies
-
User1120430333 posted
You have two ways to display the validation errors.
https://mattrandle.me/viewing-entityvalidationerrors-in-visual-studio/
To me, it's a drawback in letting Mr. Wizard do things instead rolling your own code.
Wednesday, May 22, 2019 7:42 AM -
User1520731567 posted
Hi demoninside9,
According to your error message,This may be caused by restrictions or constraints in your model.
You could use try..catch..or check Quick Watch window in controller to look for the cause.
For example:
More details,you could refer to:
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 23, 2019 8:33 AM -
User1052662409 posted
You could use try..catch..or check Quick Watch window in controller to look for the cause.I already did this.
As I see the The Author_id field is required.
This is the main drawback of EF, if you want to update only few fields you have to update all fields in database table. (When you use Controller with view and entity framework).
I am looking for the solution that any fields don't have any compulsion. Fields should be free and independent for any (CRUD) operations.
any way Thanks
Thursday, May 23, 2019 9:23 AM -
User1120430333 posted
This is the main drawback of EF, if you want to update only few fields you have to update all fields in database table. (When you use Controller with view and entity framework).
All ORM(s) work in the same manner when it comes to taking a persistence object and persisting it to the database table, which it's either all or nothing.
I am looking for the solution that any fields don't have any compulsion. Fields should be free and independent for any (CRUD) operations.
You can't do it with using a ADO.NET datatable either
The only way you could do such a thing would be able to develop a persistence object that kept its state, and it also kept all the properties states within the object for when logic you wrote to know when the persistence object was in a dirty state and what properties in the object were in a dirty state in order to formulate dynamic T-SQL based on the persistence object's state and the state of properties within the persistence object.
Thursday, May 23, 2019 1:12 PM -
User475983607 posted
As I see the The Author_id field is required.
This is the main drawback of EF, if you want to update only few fields you have to update all fields in database table. (When you use Controller with view and entity framework).
I am looking for the solution that any fields don't have any compulsion. Fields should be free and independent for any (CRUD) operations.
You are assuming how EF works rather than taking the time to understand how it works. Anyway, the solution (programming pattern) was covered in your threads from yesterday.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 23, 2019 1:38 PM -
User1520731567 posted
Hi demoninside9,
This is the main drawback of EF, if you want to update only few fields you have to update all fields in database table. (When you use Controller with view and entity framework).
No,EF will not be so rigid.There are definitely other ways to implement your function.
According to your previous post,
I suggest you could use TryUpdateModel,because the Bind attribute clears out any pre-existing data in fields not listed in the Include parameter.
[HttpPost, ActionName("Edit")] [ValidateAntiForgeryToken] public ActionResult EditPost(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var studentToUpdate = db.Students.Find(id); if (TryUpdateModel(studentToUpdate, "", new string[] { "LastName", "FirstMidName", "EnrollmentDate" }))
//you want to be updateable by the Edit page are whitelisted in the TryUpdateModel parameters. { try { db.SaveChanges(); return RedirectToAction("Index"); } catch (DataException /* 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(studentToUpdate); }More details about Bind,TryUpdateModel and so on,you could refer to:
Best Regards.
Yuki Tao
Friday, May 24, 2019 2:27 AM -
User1052662409 posted
You can't do it with using a ADO.NET datatable eitherI did by using ADO.NET
Please have a look.
Below is my DataAccessLayer
public void OTBand_Update(OTBandModel obj) { Hashtable param = new Hashtable(); param.Add("@vcname", obj.Name); param.Add("@vcrate_phour", obj.RatePerHour); param.Add("@updatedby", obj.ModifiedBy); param.Add("@bigid", obj.id); param.Add("@Case", 2); oDBHelper.Insert_Update_Delete("usp_ot_band_master_detail", param); }
Below is my ActionResult in controller.
[HttpPost] public ActionResult Edit(OTBandModel OTBand) { try { OTBand.ModifiedBy = Convert.ToInt32(Session["AdminID"].ToString()); DataAccessLayer db = new DataAccessLayer(); db.OTBand_Update(OTBand); return RedirectToAction("Index"); } catch (Exception ex) { throw; } }
Below is my Store Procedure
IF @Case = 2 -- update BEGIN UPDATE OT_Band_Master SET vcname =@vcname ,vcrate_phour=@vcrate_phour,datemodi=GetDate(),updatedby=@updatedby WHERE bigid=@bigid END
But I did not understand why you said that with ADO.Net it can not be done. May be you are trying to say something else or might be possible I misunderstood.
Friday, May 24, 2019 3:21 AM -
User1120430333 posted
But I did not understand why you said that with ADO.Net it can not be done. May be you are trying to say something else or might be possible I misunderstood.
I don't see an ADO.NET tableadapter or a dataadapter along with a dataset and datatable being used in your code, which the 'all or nothing' statement would apply for data persistent using the technology in the scenario. ADO.NET would persist the data from the datatable row and doing it column by column to the database table not concerned if the data actually changed or not for any given datatable row and column within the dataset
Friday, May 24, 2019 8:36 AM -
User1052662409 posted
DA924
I don't see an ADO.NET tableadapter or a dataadapter along with a dataset and datatable being used in your codeI am using dbhelper class Insert_Update_Delete (oDBHelper.Insert_Update_Delete("usp_ot_band_master_detail", param)). Which have the following methods.
public DataTable GetDatatable(string procName) { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); cmd.CommandText = procName; cmd.CommandType = CommandType.StoredProcedure; if (con == null) { SetConnection(); } cmd.Connection = con; da.SelectCommand = cmd; da.Fill(dt); return dt; } public int Insert_Update_Delete(string procName, Hashtable parms) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = procName; if (parms.Count > 0) { foreach (DictionaryEntry de in parms) { cmd.Parameters.AddWithValue(de.Key.ToString(), de.Value); } } if (con == null) { SetConnection(); } cmd.Connection = con; if (con.State == ConnectionState.Closed) con.Open(); int result = cmd.ExecuteNonQuery(); return result; }
Friday, May 24, 2019 9:12 AM -
User1120430333 posted
No matter how you use the ADO.NET datatable, there is sill no way that you can determine that data in a datatable row and column was modified and code is only checking for a modified column that will be persisted to a database table using an adapter, which is not unlike using an EF entity object where the persistence properties in the object are not cheeked as to what property changed with T-SQL generated for only changed data in properties by the EF engine. EF doesn't care if a property in the object has changed or not The same logic is applied to a datatable and T-SQL generated by the usage of a datatable sitting inside a dataset using an adapter or a helper that is generating the T-SQL
In either case in using a data container such as an EF entity or ADO.NET datatable all of the data in the container is being persisted with no checking if data has been modified.
It is the point I am making.
Friday, May 24, 2019 10:02 AM