I am getting the error like above tag which will be at the place of
return View(st.employees.Find(id));
above place only ,can any one help me from this! and my code is
namespace StartApp.Controllers
{
public class EmployController : Controller
{
StartEntities st = new StartEntities();
//List
public ActionResult List()
{
return View(st.employees.ToList());
}
//Details
public ActionResult Details(int id = 0)
{
return View(st.employees.Find(id));
}
//Create
public ActionResult Create()
{
return View();
}
[HttpPost,ValidateAntiForgeryToken]
public ActionResult Create(employee e)
{
using(st)
{
st.employees.Add(e);
try
{
st.SaveChanges();
}
catch
{
System.Diagnostics.Debug.WriteLine("Here is an error");
}
}
return RedirectToAction("List");
}
//edit
public ActionResult Edit(int id = 0)
{
return View(st.employees.Find(id));
}
[HttpPost,ValidateAntiForgeryToken]
public ActionResult Edit(employee e)
{
st.Entry(e).State = EntityState.Modified;
st.SaveChanges();
return RedirectToAction("List");
}
//Delete
public ActionResult Delete(int id = 0)
{
return View(st.employees.Find(id));
}
[HttpPost,ActionName("Delete")]
public ActionResult Delete_conf(int id)
{
employee emp = st.employees.Find(id);
st.employees.Remove(emp);
st.SaveChanges();
return RedirectToAction("List");
}
}
}
can any one help me to rectify that error!
This exception usually happens when your entities primary key is of type A and you are passing a variable which is not of type A to the Find
method.
From the official documentation of Find
method, It may throw the below exception
InvalidOperationException
Thrown if the types of the key values do not match the types of the key values for the entity type to be found.
Make sure you use the same type variable when you call the Find
method.
In your code, you are passing an integer variable to the Find
method. From the error i believe your entity classes primary key is not int type. May be it is Guid
type, in that case, make sure you are passing a valid Guid value to the Find
method.
You can open up the edmx file and see the type of your key and make sure you pass the same type to the Find
method.
Just right click on the entity in your edmx file and select properties.