C# 如何在MVC5中的数据库中创建新表?

C# 如何在MVC5中的数据库中创建新表?,c#,mysql,asp.net-mvc,database,C#,Mysql,Asp.net Mvc,Database,我有一个MVC5应用程序,它使用Azure中托管的MySQL作为数据源。我遵循了这个,现在我有了这些。关键是在数据库中,我想创建一个名为“request”的新表。我已经在代码中为我的数据库激活了迁移。我的应用程序中也有以下代码 Request.cs:(位于模型文件夹内) Test.cshtml: @model Workfly.Models.Request @{ ViewBag.Title = "Test"; } <h2>@ViewBag.Title.</h2>

我有一个MVC5应用程序,它使用Azure中托管的MySQL作为数据源。我遵循了这个,现在我有了这些。关键是在数据库中,我想创建一个名为“request”的新表。我已经在代码中为我的数据库激活了迁移。我的应用程序中也有以下代码

Request.cs:(位于模型文件夹内)

Test.cshtml:

@model Workfly.Models.Request
@{
    ViewBag.Title = "Test";
}

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@using (Html.BeginForm("SaveAndShare", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new request.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.RequestType, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.RequestType, new { @class = "form-control", @id = "keywords-manual" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Submit!" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
    [HttpPost]
    public ActionResult SaveAndShare(Request request)
    {
        if (ModelState.IsValid)
        {
            var req = new Request { RequestType = request.RequestType };
        }

        return RedirectToAction("Share");
    }
关键是,我希望用户在测试视图中填写表单并单击submit,当单击submit时,我希望在新表中创建一个新条目。但首先,我当然需要创建表。我应该通过MySQL工作台使用SQL查询创建它吗?如果是,那么如何使用代码连接新表?我想我需要一些数据库上下文,但不知道怎么做。如果有人能发布一些代码示例,我会很高兴

更新:

@model Workfly.Models.Request
@{
    ViewBag.Title = "Test";
}

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@using (Html.BeginForm("SaveAndShare", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new request.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.RequestType, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.RequestType, new { @class = "form-control", @id = "keywords-manual" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Submit!" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
    [HttpPost]
    public ActionResult SaveAndShare(Request request)
    {
        if (ModelState.IsValid)
        {
            var req = new Request { RequestType = request.RequestType };
        }

        return RedirectToAction("Share");
    }
我在Models文件夹中创建了一个新类,并将其命名为RequestContext.cs,其内容如下所示:

public class RequestContext : DbContext
{
    public DbSet<Request> Requests { get; set; }
}

}

必须创建DbContext类,并添加引用请求类的数据库集。然后使用控制台添加迁移和更新数据库


删除当前的迁移,添加db上下文类和dbset以引用请求类,然后再次添加迁移。尝试在azure上部署时遇到了类似的问题,并且成功了

您在Package Manager控制台上尝试过“更新数据库”命令吗?@Gandarez是的,它不起作用,请检查我的更新。