我已經搜索了一個解決方案來解決我手邊的障礙幾天,但是我還沒有找到需要繼續的東西,我被卡住了!插入表格中的數據庫中的多個表
我想要做的是將「firstName」,「lastName」和「subject」註冊到數據庫中的兩個表中,其中一個表包含名和姓。第二張表只包含課程。
我發現在stackoverflow中有很多類似於這個主題的帖子,但由於某些原因,我無法解決這些例子。
的視圖:
<form action="/Default/Page" method="post">
<input type="text" name="firstName" />
<input type="text" name="lastName" />
<input type="text" name="subject" />
<input type="submit" value="submit" />
</form>
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;
using System.Data;
using System.Data.Entity;
namespace WebApplication2.Controllers
{
public class DefaultController : Controller
{
[HttpPost]
public ActionResult page(FormCollection collection)
{
App2Context db = new App2Context();
Model dat = new Model();
dat.Pupil.First = collection["firstName"];
dat.Pupil.First = collection["firstName"];
dat.Study.Subject = collection["Subject"];
return View("Page2");
}
}
}
模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.Models
{
public class Model
{
public Name Pupil { get; set; }
public Course Study { get; set; }
}
public class Name
{
public string First { get; set; }
public string Last { get; set; }
}
public class Course
{
public string Subject { get; set; }
}
}
的的DbContext類包含兩個DbSets它們的名稱和課程。
我到目前爲止所嘗試的是使用上下文類中的對象,但智能感知只顯示db.savechanges()它沒有向我提供將對象dat添加到數據庫中的方法。我嘗試了一些LINQ查詢,但由於我對查詢不太熟悉,所以我只是被卡住了。
查詢:
var data = from s in db.Names where dat.Pupil.First equals
然後我的IDE只是有紅色的波浪線,並且不顯示任何的IntelliSense支持。
如何使用實體框架將我試圖通過表單收集的信息輸入數據庫中的兩個表中?
如何使用LINQ查詢將相同的信息輸入到表中?
我收到一個錯誤System.Data.Entity.Infrastructure。DbUpdateException:更新條目時發生錯誤 – venichhe 2014-10-07 17:44:43
它解釋了你有其他結構在表和其他結構你想要發送到數據庫。請詳細說明。 – W92 2014-10-07 18:23:19
更好的方式來創建兩個表格:名稱和課程 – W92 2014-10-07 19:22:44