此操作的参数类型'Edm.String'和'Edm.Int32'不兼容 [英] The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation

查看:701
本文介绍了此操作的参数类型'Edm.String'和'Edm.Int32'不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到类似上面的标记的错误,它将出现在

的位置

返回视图(st.employees.Find(id));

仅在以上位置,任何人都可以帮助我!我的代码是

     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");
    }

}

}

任何人都可以帮助我纠正该错误!

解决方案

当您的实体主键为A类型并且您将非A类型的变量传递给Find方法时,通常会发生此异常. /p>

来自官方文档 Find方法,它可能会引发以下异常

InvalidOperationException

键值的类型与键值的类型不匹配时抛出 要找到的实体类型的关键值.

请确保在调用Find方法时使用相同的类型变量.

在您的代码中,您正在将一个整数变量传递给Find方法.从错误中我相信您的实体类的主键不是int类型.可能是Guid类型,在这种情况下,请确保将有效的Guid值传递给Find方法.

您可以打开edmx文件,查看密钥的类型,并确保将相同的类型传递给Find方法.

只需右键单击edmx文件中的实体,然后选择属性.

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.

这篇关于此操作的参数类型'Edm.String'和'Edm.Int32'不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆