2016-04-28 11 views
0

//アップロードされたファイルを読み込むコントローラです.App_Dataにファイルをアップロードすると正常に動作しますが、MS SQL Serverにアップロードすると「無効オブジェクトdbo.Prescription "//ここエンティティに新しいレコードを追加すると、DbUpdateExceptionが返されます

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持っている" Medicinをe "と" Prescription " // 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; } 
} 
} 

答えて

0

はあなたのエンティティで、それはそう、あなたのデータベースであなたのテーブルのあるべきDBOだ、

あなたエンティティが処方され、あなたのテーブル名が処方すること、そしてまた、あなたのテーブルのスキーマをチェックする必要があり、あなたのデータベースにあなたのテーブル名を確認してください同じスキーマ。

+0

テーブル名は「処方箋」 –

+0

です。テーブル名は、モデル名「処方箋」が正しいと感じる私のdbの "dbo.Prescription"です。 –

+0

あなたのテーブルの名前をPrescriptionsに変更するか、モデルのテーブル属性をに追加してください。 デフォルトでは、EFはマッピングに複数の名前を使用します。 –

関連する問題