错误:此上下文仅支持基元类型或枚举类型

时间:2015-06-04 06:29:12

标签: asp.net-mvc-4

[HttpPost]
    public ActionResult Dep_Save_Attachments(int in_queue_id, HttpPostedFileBase httpfile, string authorized_by, string authorized_to, string confirmed_by, string approved_by)
    {
        if (httpfile != null)
        {

            var folder = Server.MapPath("~/Attachments/Laptops/" + in_queue_id + "/");
            var prev_fa = db.File_Attachments.Where(x => x.inventory_table_id == in_queue_id).Where(x => x.is_active == true).ToList();
            var prev_hfa = db.HW_Authorization_Forms.Where(x => x.file_attachments_id == prev_fa.FirstOrDefault().file_attachments_id).Where(x => x.is_active == true).ToList();

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            if (prev_fa.Count != 0)
            {
                foreach (var pf in prev_fa)
                {
                    pf.is_active = false;

                    db.Entry(pf).State = EntityState.Modified;
                    db.SaveChanges(); ;
                }
            }

            if (prev_hfa.Count != 0)
            {
                foreach (var hpf in prev_hfa)
                {
                    hpf.is_active = false;

                    db.Entry(hpf).State = EntityState.Modified;
                    db.SaveChanges(); ;
                }
            }



            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/Laptops/" + in_queue_id + "/") + System.IO.Path.GetFileName(httpfile.FileName);
                httpfile.SaveAs(path);

                File_Attachments fa = new File_Attachments();

                fa.file_attachments_id = 1;
                fa.inventory_table_name = "Laptops_Transactions";
                fa.inventory_table_id = in_queue_id;
                fa.file_name = System.IO.Path.GetFileName(httpfile.FileName);
                fa.file_path = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Attachments/Laptops/" + httpfile.FileName;
                fa.created_by = @User.Identity.Name.Remove(0, 9).ToLower();
                fa.created_date = System.DateTime.Now;
                fa.is_active = true;

                db.File_Attachments.Add(fa);
                db.SaveChanges();

                Laptops_Transactions laptops_trans = db.Laptops_Transactions.Find(in_queue_id);
                laptops_trans.lp_trans_type = "deployed";
                laptops_trans.lp_date_returned = System.DateTime.Now;

                db.Entry(laptops_trans).State = EntityState.Modified;
                db.SaveChanges();

                HW_Authorization_Forms hwf = new HW_Authorization_Forms();
                hwf.hw_authorization_forms_id = 1;
                hwf.file_attachments_id = fa.file_attachments_id;
                hwf.hw_authorized_by = authorized_by;
                hwf.hw_authorized_to = authorized_to;
                hwf.hw_confirmed_by = confirmed_by;
                hwf.hw_approved_by = approved_by;
                hwf.hw_approved_date = fa.created_date;
                hwf.created_by = fa.created_by;
                hwf.created_date = fa.created_date;
                hwf.hw_authorized_date = fa.created_date;
                hwf.hw_confirmed_date = fa.created_date;
                hwf.is_active = true;

                db.HW_Authorization_Forms.Add(hwf);
                db.SaveChanges();

            }
            catch
            {

            }
        }
        else
        {

            return Content("<script language='javascript' type='text/javascript'>alert('Please Attach the Deployment Authorization Form! Kindly go back to previous page');</script>");
        }

     return RedirectToAction("Index");

    }

错误在这一行:

var prev_hfa = db.HW_Authorization_Forms.Where(x => x.file_attachments_id == prev_fa.FirstOrDefault().file_attachments_id).Where(x => x.is_active == true).ToList();

这是我在控制器中的代码,实际上这已经有效但它突然出错了。我真的不知道为什么在它完美运作之前我会遇到这种错误。

请帮我解决这个问题。需要一些建议。提前谢谢。

1 个答案:

答案 0 :(得分:0)

错误是由于数据类型问题,我猜你需要确认你正在使用正确的数据类型file_attachments_id数据库,并且你的比较值必须相同。

此外,is_active必须是数据类型Boolean。更正此问题可能会解决您的错误。