[英]Entity Framework Core - Microsoft.Data.SqlClient.SqlException : Invalid column name
[英]Microsoft.Data.SqlClient.SqlException - Invalid object name 'GuestList'.'
我的数据库有一个大问题,我无法从 web 页面写入。 我正在使用 Dapper 作为我的 ORM 如果这有什么不同?
db.Insert(guest.EditableGuest); 给了我无效的 object 错误 但如果我将其更改为 db.InsertAsync(guest.EditableGuest); 该错误自行解决但我无法将其添加到数据库中,并且由于某种原因它不会显示在我在数据库中有客人列表的页面上?
我决定重新输入页面以防出现问题,VS有时似乎有点挑剔,有一天我出错了,关闭应用程序,重新打开它就可以了? 不幸的是,没有用这个。
访客列表控制器
using Dapper.Contrib.Extensions;
using FYP_RSVP_MGMT.Helpers;
using FYP_RSVP_MGMT.Models;
using FYP_RSVP_MGMT.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace FYP_RSVP_MGMT.Controllers
{
public class GuestListController : Controller
{
public IActionResult Index()
{
GuestListViewModel guest = new GuestListViewModel();
return View("Index", guest);
}
/* Create or Update a guest RSVP Response */
public IActionResult CreateUpdate(GuestListViewModel guest)
{
if (ModelState.IsValid)
{
using (var db = DbHelpers.GetConnection())
{
/* If a guestID is null, the number of existing guests will be counted
* in order to determine what the next guestID will be and will be added
* asynchronously to the DB in case other actions are on going at the same time */
if (guest.EditableGuest.GuestID == null)
{
guest.EditableGuest.GuestID = guest.Guests.Count;
db.Insert<GuestList>(guest.EditableGuest);
}
/* If the guest already exists, we are updating their details */
else
{
GuestList dbItem = db.Get<GuestList>(guest.EditableGuest.GuestID);
TryUpdateModelAsync<GuestList>(dbItem, "EditableGuest");
db.Update<GuestList>(dbItem);
}
}
/* When a guest submits their RSVP response, it will bring them to the View Guests page - TEMPORARY MEASURE */
return RedirectToAction("ViewGuestList", guest);
}
else
{
return View("ViewGuestList", new GuestList());
}
}
除此之外,还有更多用于编辑和删除的代码,并且页面没有抛出任何错误,所以一切都很好。
这是我的数据库助手 class
public class DbHelpers
{
public static SqlConnection GetConnection()
{
return new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=WeddingDB;");
}
}
嘉宾名单 Model
public class GuestList
{
[ExplicitKey]
public int? GuestID { get; set; }
[Required]
public string GuestName { get; set; }
[Required]
public string GuestType { get; set; }
[Required]
public string ContactDetails { get; set; }
public bool PlusOne { get; set; }
public string PlusOneName { get; set; }
[Required]
public string GuestResponse { get; set; }
}
访客列表视图模型
public class GuestListViewModel
{
/* Creating a new List of all Guests
Editable Guest will hold any instance of an object from the list
that is being added/edited/deleted etc */
public List<GuestList> Guests { get; set; }
public GuestList EditableGuest { get; set; }
/* Selecting all of the existing guests from the DB */
public GuestListViewModel()
{
using (var db = DbHelpers.GetConnection())
{
this.EditableGuest = new GuestList();
this.Guests = db.Query<GuestList>("Select * From GuestList").ToList();
}
}
}
来宾列表视图
@model FYP_RSVP_MGMT.ViewModels.GuestListViewModel
@{
ViewData["Title"] = "Guest RSVP";
}
@* Form to submit RSVP Response *@
@using (var form = Html.BeginForm("CreateUpdate", "GuestList", FormMethod.Post))
{
<div class="container" id="GuestRSVP">
<br />
<br />
<br />
@* The Bride and Groom name plus Wedding Details will eventually change
depending on the log in details *@
<h3> Welcome to [Bride] and [Groom]'s Wedding</h3>
<h4>[Church Location]</h4>
<h4>[Wedding Date and Time]</h4>
<div class="container" id="RSVPTable" style="align-content:center">
<table>
<tr>
<td>Guest Name: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.GuestName)</td>
</tr>
<tr>
<td>Guest Type: </td>
<td>@Html.DropDownListFor(m => m.EditableGuest.GuestType, new List<SelectListItem> { new SelectListItem { Value = "Guest", Text = "Guest" }, new SelectListItem { Value = "Wedding Party", Text = "Wedding Party" } })</td>
</tr>
<tr>
<td>Email Address: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.ContactDetails)</td>
</tr>
<tr>
<td>Plus One: </td>
<td>@Html.CheckBoxFor(m => m.EditableGuest.PlusOne)</td>
</tr>
<tr>
<td>Plus One Name: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.PlusOneName)</td>
</tr>
<tr>
<td>RSVP Response:</td>
<td>@Html.DropDownListFor(m => m.EditableGuest.GuestResponse, new List<SelectListItem> { new SelectListItem { Value = "Accept", Text = "Accept with Pleasure" }, new SelectListItem { Value = "Decline", Text = "Decline with Regret" } })</td>
</tr>
</table>
<br/>
<button class="btnSubmitRSVP" type="submit" style="text-align:center"> <a asp-controller="GuestList" asp-action="CreateUpdate"></a> @(Model.EditableGuest.GuestID > 0? "Update": "Submit Response") </button>
</div>
</div>
}
我发送某些页面的位置可能有点奇怪,就像客人 RSVPing 不应该被带到他们可以看到其他客人和他们的回复的页面,但这只是暂时的。 这会影响我的代码吗,肯定不会? 我的代码中没有任何地方有 GuestLists,所以我不明白它在哪里捡到这个? 如果需要,我可以提供更多代码。
提前谢谢你的帮助!
Dapper 假设一个复数表名。 在 GuestList model class 中使用 '[Table("GuestList")]' 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.