The property identifier "ID" is part of the key information of the object and cannot be changed

I am trying to run the MVC unit test for an action that clones a record. Naturally, we use the primary key of the master record to retrieve it from the database first, using a simple one DbSet.Find(). Right now I have written about 100 unit tests, and 98/100 pass, including a number of them, necessary to test various aspects of transactions of this type ... and they all pass.

At the first stage, the user decided to create Agency, but chose that they also would like to add another. The page will return to the controller method Create POSTand save.

Once the save is complete, the program is designed to re-route back to the method Create GET. From there, it collects relevant information and sends the new model object back to the view.

var result = _controlAgtTran.Create(_agency, "andAddAnother") as RedirectToRouteResult;
var message = _controlAgtTran.Create(int.Parse(result.RouteValues["id"].ToString()), null, null, null, null, null, true, result.RouteValues["msg"].ToString()) as ViewResult;

Nothing complicated happens when we are redirected to action GET, as you can see below. We simply call the method DbSet.Find(<PK>)to capture the just saved object.

AgentTransmission master = db.AgentTransmission.Find(id);

However, each time, and only for this operation, the following error message appears. I went through the code to make sure that the variable is idnot null, I requested the database to make sure it was fixed, and I compared this part of unit test with the others in the solution and can find the difference. Any help / suggestions would be greatly appreciated.

.

enter image description here

+4

Source: https://habr.com/ru/post/1539025/


All Articles