如何使用 ASP.NET MVC 2 更改外键实体

发布于 2024-11-10 15:22:46 字数 1750 浏览 4 评论 0原文

[HttpPost]
    public ActionResult Edit(FormCollection form)
    {
        // Get movie to update
        var id = Int32.Parse(form["id_foto"]);
        var fotoToAdd = _db.foto.First(m => m.id_foto == id);

        // Deserialize (Include white list!)
        TryUpdateModel(fotoToAdd, new string[] { "descricao" },    form.ToValueProvider());

        //Here I try to change my foreign key, but i get this exception: "The property 'id_album' part of the key information of the object and can not be changed."
        fotoToAdd.id_album = Convert.ToInt32(form["id_album"]);

        //file upload 
        if (Request.Files.Count > 0)
        {
            int tamanho = (int)Request.Files[0].InputStream.Length;
            byte[] arq = new byte[tamanho];
            Request.Files[0].InputStream.Read(arq, 0, tamanho);
            byte[] arqUp = arq;
            fotoToAdd.imagem = arqUp;
        }

        //Validation
        if (String.IsNullOrEmpty(fotoToAdd.descricao))
            ModelState.AddModelError("Descrição", "Ops... campo obrigatório");
        if (String.IsNullOrEmpty(fotoToAdd.id_album.ToString()))
            ModelState.AddModelError("Album", "Ops... campo obrigatório");
        if (fotoToAdd.imagem == null)
            ModelState.AddModelError("Foto", "Ops... campo obrigatório");

        //If success, Update
        if (ModelState.IsValid)
        {
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        //Else, return view
        return View(fotoToAdd);
    }

//在这里,我尝试更改我的外键,但出现此异常:“属性‘id_album’是对象的关键信息的一部分,无法更改。”

fotoToAdd.id_album = Convert.ToInt32(form["id_album"]);

我一直在研究如何执行此操作,但遇到困难,如何进行此更新操作

[HttpPost]
    public ActionResult Edit(FormCollection form)
    {
        // Get movie to update
        var id = Int32.Parse(form["id_foto"]);
        var fotoToAdd = _db.foto.First(m => m.id_foto == id);

        // Deserialize (Include white list!)
        TryUpdateModel(fotoToAdd, new string[] { "descricao" },    form.ToValueProvider());

        //Here I try to change my foreign key, but i get this exception: "The property 'id_album' part of the key information of the object and can not be changed."
        fotoToAdd.id_album = Convert.ToInt32(form["id_album"]);

        //file upload 
        if (Request.Files.Count > 0)
        {
            int tamanho = (int)Request.Files[0].InputStream.Length;
            byte[] arq = new byte[tamanho];
            Request.Files[0].InputStream.Read(arq, 0, tamanho);
            byte[] arqUp = arq;
            fotoToAdd.imagem = arqUp;
        }

        //Validation
        if (String.IsNullOrEmpty(fotoToAdd.descricao))
            ModelState.AddModelError("Descrição", "Ops... campo obrigatório");
        if (String.IsNullOrEmpty(fotoToAdd.id_album.ToString()))
            ModelState.AddModelError("Album", "Ops... campo obrigatório");
        if (fotoToAdd.imagem == null)
            ModelState.AddModelError("Foto", "Ops... campo obrigatório");

        //If success, Update
        if (ModelState.IsValid)
        {
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        //Else, return view
        return View(fotoToAdd);
    }

//Here I try to change my foreign key, but i get this exception: "The property 'id_album' part of the key information of the object and can not be changed."

fotoToAdd.id_album = Convert.ToInt32(form["id_album"]);

I have been researching how to do this, but am having difficulty, how do I make this update operation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文