2016-05-19 12 views
0

私はMVCを初めて使用していますが、正しい質問をしてもわかりませんが、EFを使用していてDBContextを使用するチュートリアルに従っていれば、 CRUD操作を可能にするビューに表示するEF。Entity FrameworkでDBContextを使用する方法

Model: 

[Table("ProjectInformation")] 
public class ProjectDetailsViewModels 
{ 
    [Key] 
    public int pkiProjectID { get; set; } 
    public string ProjectName { get; set; } 
    public string DesignerName { get; set; } 
    public string ProjectType { get; set; } 
    public string FluidType { get; set; } 
    public string PipeMaterial { get; set; } 
} 


public class ProjectDBContext : DbContext 
{ 
    public DbSet<ProjectDetailsViewModels> Projects { get; set; } 
} 

以下は私のコントローラです。私はCreateメソッドを追加しました。

Invalid object name 'dbo.ProjectInformation'. 

だから私は読んで別のポストに、私は私のモデルに続いて追加により:

以下
View : 

@model IEnumerable<AirFlo_Size_Programme.Models.ProjectDetailsViewModels> 
@{ 
ViewBag.Title = "Index"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

<p> 
@Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.ProjectName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.DesignerName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.ProjectType) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.FluidType) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.PipeMaterial) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ProjectName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.DesignerName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ProjectType) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.FluidType) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.PipeMaterial) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.pkiProjectID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.pkiProjectID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.pkiProjectID }) 
     </td> 
    </tr> 
} 

</table> 

が取得エラーイムある:以下
Controller: 

public class ProjectDetailController : Controller 
{ 

    private ProjectDBContext db = new ProjectDBContext(); 

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

    // GET: Movies/Details/5 
    public ActionResult Details(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     ProjectDetailsViewModels ProjectModel = db.Projects.Find(id); 
     if (ProjectModel == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(ProjectModel); 
    } 

    // GET: Movies/Create 
    public ActionResult Create() 
    { 
     return View(); 
    } 

    // POST: Movies/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "pkiProjectID,ProjectName,DesignerName,ProjectType,FluidType, PipeMaterial")] ProjectDetailsViewModels ProjectModel) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Projects.Add(ProjectModel); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(ProjectModel); 
    } 
} 

は私のビューであります

Iveがチェックされ、これらの各テーブルには主キーがあります。

どこに間違っているのか/間違っているのか教えてください。

ありがとうございます!

+0

されるべきだと思ういかなる意味

がありません'pkiProjectID'には[Key]属性があります。 –

+0

クイック返信をありがとう。私は両方を試して、同じエラーが発生しています – AxleWack

+0

あなたはProjectInformationモデルを含んでいませんでした。 –

答えて

0

プライマリキーフィールドには[Key]属性を指定する必要があります。その後、Entity Frameworkのみがそれをプライマリキーとして認識します。

public class ProjectDetailsViewModels 
{ 
    [Key] 
    public int pkiProjectID { get; set; } 
    public string ProjectName { get; set; } 
    public string DesignerName { get; set; } 
    public string ProjectType { get; set; } 
    public string FluidType { get; set; } 
    public string PipeMaterial { get; set; } 
} 

また、間違った方法で他のフィールドを定義していると思います。

public string ProjectName { get; set; } 

これは、私はそれがProjectDetailsViewModels` `にちょうど`公共のint Id`を試してみてくださいまたは飾る

public ProjectName ProjectName { get; set; } 

または

public string Name { get; set; } 
関連する問題