The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation

asp.net-mvc c# entity-framework linq-to-entities sql-server

Question

I am seeing an error similar to the one in the preceding tag at the location of

return View(st.employees.Find(id));

Please assist me if you can at the aforementioned location alone. the code I use 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 someone please assist me in fixing that mistake?

1
3
8/8/2016 3:16:46 PM

Accepted Answer

This exception often occurs when you send a variable that isn't of type A to the entity whose main key is of type A.Find method.

between the zzz-12 and zzz ofFind method, it might raise the following 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.

When calling the, be sure to use the same type variable.Find method.

You are supplying an integer variable to the in your code.Find method. I think your entity classes' main key is not an int type based on the problem. Perhaps it isGuid ensure that you are sending a valid Guid value to the type in that situation.Find method.

You can see the type of your key in the edmx file by opening it. Ensure that you provide the same type to theFind method.

Simply choose properties by doing a right-click on the item in your edmx file.

enter image description here

5
8/8/2016 3:43:40 PM

Popular Answer

You seem to be using the MVC design pattern.

I had the same problem because I sent the id parameter as an integer rather than as a "string," as I had specified in my model.

Sample:

public class Object
{
    public string id { get; set; }
    public string property1{ get; set; }
    public string property2{ get; set; }
    public string property3{ get; set; }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow