2014-09-19 59 views
-2

我使用ASP.NET MVC創建了一個小型網站,但它無法從數據庫創建新的,編輯或刪除數據。數據只顯示在網頁上,但是當我在SQL中使用SELECT *命令時,數據不顯示。爲什麼我的ASP.NET MVC無法將數據插入到我的數據庫中?

我在webconfig連接字符串:

<add name="CodeFileDBContext" 
    providerName="System.Data.SqlClient" 
    connectionString="Data Source=HOANG-PC\SQLSERVER01;Initial Catalog=Ciaos;User Id=sa;Password=**********;MultipleActiveResultSets=True" /> 

型號:

namespace Ciao.Models 
{ 
    public class CodeFile 
    { 
     [Key] 
     public int ColdeFile_ID { get; set;} 
     public string Website_Name { get; set;} 
     public string Service_Name { get; set;} 
     public DateTime Date_In { get; set;} 
     public DateTime Date_Out { get; set;} 
     public int Service_Status { get; set;} 

    } 
    public class CodeFileDBContext : DbContext 
    { 
     public DbSet<CodeFile> tbl_CodeFile { get; set; } 
    } 
} 

控制器:

namespace Ciao.Controllers 
{ 
    public class CodeFileController : Controller 
    { 
     private CodeFileDBContext db = new CodeFileDBContext(); 

     // 
     // GET: /CodeFile/ 

     public ActionResult Index() 
     { 
      return View(db.tbl_CodeFile.ToList()); 
     } 

     // 
     // GET: /CodeFile/Details/5 

     public ActionResult Details(int id = 0) 
     { 
      CodeFile codefile = db.tbl_CodeFile.Find(id); 
      if (codefile == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(codefile); 
     } 

     // 
     // GET: /CodeFile/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /CodeFile/Create 

     [HttpPost] 
     public ActionResult Create(CodeFile codefile) 
     { 
      if (ModelState.IsValid) 
      { 
       db.tbl_CodeFile.Add(codefile); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(codefile); 
     } 

     // 
     // GET: /CodeFile/Edit/5 

     public ActionResult Edit(int id = 0) 
     { 
      CodeFile codefile = db.tbl_CodeFile.Find(id); 

      if (codefile == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(codefile); 
     } 

     // 
     // POST: /CodeFile/Edit/5 

     [HttpPost] 
     public ActionResult Edit(CodeFile codefile) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(codefile).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(codefile); 
     } 

     // 
     // GET: /CodeFile/Delete/5 

     public ActionResult Delete(int id = 0) 
     { 
      CodeFile codefile = db.tbl_CodeFile.Find(id); 
      if (codefile == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(codefile); 
     } 

     // 
     // POST: /CodeFile/Delete/5 

     [HttpPost, ActionName("Delete")] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      CodeFile codefile = db.tbl_CodeFile.Find(id); 
      db.tbl_CodeFile.Remove(codefile); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      db.Dispose(); 
      base.Dispose(disposing); 
     } 
    } 
} 
+0

你獲得詳細信息頁面的數據? – 2014-09-19 08:18:22

回答

0

DbContext基類接受連接字符串名稱作爲參數。嘗試通過連接字符串名稱通過CodeFileDBContext

+0

嗨。我已經在連接字符串中使用它「name =」CodeFileDBContext「」 – 2014-09-19 07:32:14

+0

對不起,我沒有注意到,你有任何異常嘗試張貼,如果你有一個。 – meetkichu 2014-09-19 07:41:43

+0

我使用數據eplorer來測試從我的網站到數據庫的連接,並且工作正常。我創建新的數據,它在網站視圖上的列表,但它不是更新數據庫時使用SQL管理工具來查看我的表數據中的數據 – 2014-09-19 07:44:58

1

添加構造你的CodeFileDBContext類:

例如像這樣:

public CodeFileDBContext() : base("Name=CodeFileDBContext") 
{ 
    var adapter = (IObjectContextAdapter)this; 
    var objectContext = adapter.ObjectContext; 
    objectContext.CommandTimeout = 30; // value in seconds 
} 
0

你能給我們更多的細節,什麼是你得到確切的錯誤? 您應該檢查的另一件事是模型中的每個數據成員都在該特定表中具有其列。如果他們的名字不完全匹配使用'Column'屬性。 舉例來說,如果它們不匹配嘗試:

[Column("Table_Id")] 
public int ID { get; set; } 

如果它們都匹配,添加「表」屬性,它以前多次幫助了我。

[Table("YourTableName")] 
{ 
public class CodeFile.... 
} 
+0

我連接到數據庫中的錯誤表原因是問題。 – 2014-09-19 09:16:02

相關問題