【问题标题】:When I add new record to the Entity,it gives DbUpdateException当我向实体添加新记录时,它会给出 DbUpdateException
【发布时间】:2016-04-28 14:20:30
【问题描述】:

//这里是读取上传文件的控制器。文件上传到App_Data时它工作成功,但是当上传到MS SQL Server时它给出错误“无效对象dbo.Prescription”

using SastiDawaai.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;

namespace SastiDawaai.Controllers
{
public class MedicineController : Controller
{

    // GET: Medicine
    public ActionResult Index()
    {
        MedicineContext medctx = null;
        using(medctx=new MedicineContext())
        {
        List<Medicine> medicinelist=medctx.Medicines.ToList();
        return View(medicinelist);
        }
    }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        MedicineContext medctx = null;
        Prescription pres = null;
        if(file!=null && file.ContentLength>0)
        {
            ////upolading files to App_Server
            var filename = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data"), filename);
            file.SaveAs(path);

            //uploading Files to database
            var file_content=new BinaryReader(file.InputStream);
            var content=file_content.ReadBytes(file.ContentLength);

                using (medctx = new MedicineContext())
                {
                    pres = new Prescription
                    {
                        Prescription_Receipt = content,
                        File_Name = filename,
                        Submitted_Date = DateTime.Now,


                    };


                    medctx.Prescriptions.Add(pres);
                    medctx.SaveChanges();

                }



        }
        return RedirectToAction("Index");
    }






}

}

//这是处方模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace SastiDawaai.Models
{

public class Prescription
{
    [Key]
    public int Prescription_Id { get; set; }
    public byte[] Prescription_Receipt { get; set; }
    public DateTime Submitted_Date { get; set; }
    public string File_Name { get; set; }
}
}

//这里是具有 2 个 DBSet “Medicine”和“Prescription”的 DBContext 类 //在 Medicine 实体中获取记录和插入记录没有问题,仅在将记录添加到添加到上下文中的任何其他 DBSet 时才会出现问题。

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace SastiDawaai.Models
{
public class MedicineContext:DbContext
{

    public virtual DbSet<Medicine> Medicines { get; set; }
    public virtual DbSet<Prescription> Prescriptions { get; set; }
}
}

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

    检查数据库中的表名

    您的实体是 Prescription 并且您的表名应该是 PrescriptionS,并且还要检查您的表架构,在您的实体中它是 Dbo,所以您的数据库中的表应该是相同的架构。

    【讨论】:

    • 表名是“处方”
    • 表名在我的数据库中是“dbo.Prescription”,我觉得模型名称“Prescription”是正确的。
    • 因此将您的表格重命名为 Prescriptions 或在您的模型中添加表格属性。默认情况下,EF 使用复数名称进行映射。
    • 当表名更改为 Prescriptions 时,出现其他错误提示“Invalid Column Name "Prescription_Id"
    • 在您的模型中您有 Prescription_Id,但在您的数据库中您没有
    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多