如何将DBContext与Entity Framework一起使用

时间:2016-05-19 14:54:28

标签: c# asp.net-mvc entity-framework dbcontext

我是MVC的新手,我不知道我是否提出了正确的问题,但我使用EF并遵循使用DBContext的教程,我想从My EF填充一些数据以显示在View中允许CRUD操作。

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; }
}

下面是我的控制器,我只添加了创建方法。

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);
    }
}

以下是我的观点:

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>

以下是我得到的错误:

Invalid object name 'dbo.ProjectInformation'. 

所以由于我读到的另一篇文章,我将以下内容添加到我的模型中:

我已经检查过,每个表都有一个主键。

让我知道我哪里出错/我做错了什么。

谢谢!

1 个答案:

答案 0 :(得分: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; }   

这没有任何意义

我认为应该是

public ProjectName ProjectName { get; set; } 

public string Name { get; set; }