//这是读取上传文件的控制器。当文件上传到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; }
}
}
//这里是DBContext类有2个DBSets“Medicine”和“Prescription” //获取记录并在医学实体中插入记录没有问题,只有在添加到上下文的任何其他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; }
}
}
答案 0 :(得分:0)
检查数据库中的表名
您的实体是Prescription,您的表名应该是PrescriptionS,并且还检查您的表模式,在您的Entity它的Dbo中,这样您的数据库中的表应该是相同的模式。