1

我有一个在 HTTPPOST 上的 db.SaveChanges() 上失败的发票的创建视图。异常消息是 {"Invalid column name 'UserDetail_UserId'."}。在 POST 控制器中,我尝试添加到 InvoiceItems 表中的 invoiceitem 对象似乎正确填充了下拉列表中选定的用户名,该用户名由我的 UserDetails 表中的用户名填充。“UserId”是 UserDetails 表中的关键列,但它没有在下拉列表中使用,所以我不清楚为什么任何数据都试图保存到“UserDetail_UserId”列中。我的代码如下:

楷模:

public class InvoiceItem
{
    //Identity Column
    public int ServiceID { get; set; }

    [Key]
    public string ProfileUserName { get; set; }

    public DateTime InvoiceDate { get; set; }
    public string Address { get; set; }
    public string Note { get; set; }
    public decimal Price { get; set; }
    public decimal SubTotal { get; set; }
    public bool InvoicePaid { get; set; }
}

public class UserDetail
{
    [Key]
    public Guid UserId { get; set; }
    public string ProfileUserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public DateTime DateUpdated { get; set; }
    public virtual ICollection<InvoiceItem> InvoiceItems { get; set; }
}

public class ProShotDB : DbContext
{
    public DbSet<UserDetail> UserDetails { get; set; }
    public DbSet<UserLinkToken> UserLinkTokens { get; set; }
    public DbSet<InvoiceItem> InvoiceItems { get; set; }
}

控制器:

public ActionResult Create()
    {
        ViewBag.ProfileUserNames = db.UserDetails.Select(
        c => new
        {
            ProfileUserName = c.ProfileUserName,
            NameAddress = c.FullName + " - " + c.Address
        });
        return View();
    }

    [HttpPost]
    public ActionResult Create(InvoiceItem invoiceitem)
    {
        decimal Tax = 1.066m;
        if (ModelState.IsValid)
        {
            invoiceitem.InvoiceDate = DateTime.Now;
            invoiceitem.SubTotal = invoiceitem.Price*Tax;
            invoiceitem.InvoicePaid = false;

            db.InvoiceItems.Add(invoiceitem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.ProfileUserNames = db.UserDetails.Select(
        c => new
        {
            ProfileUserName = c.ProfileUserName,
            NameAddress = c.FullName + " - " + c.Address
        });
        return View(invoiceitem);
    }

看法:

@model Pro_Shot_Portal.Models.InvoiceItem

@{
    ViewBag.Title = "Input Invoice";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Input Invoice:</legend>

    <div class="alert alert-error" style="width:250px;">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>Warning!</strong> Assigning invoice to wrong user could cause major problems, please triple check before proceeding.
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.ProfileUserName, "Attach invoice to user")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.ProfileUserName, new SelectList(ViewBag.ProfileUserNames, "ProfileUserName", "NameAddress"), "-- Pick Client For Invoice --")
        @Html.ValidationMessageFor(model => model.ProfileUserName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address)
        @Html.ValidationMessageFor(model => model.Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Note)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Note)
        @Html.ValidationMessageFor(model => model.Note)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Price)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Price)
        @Html.ValidationMessageFor(model => model.Price)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Cancel Invoice Creation", "Index")
</div>

据我所知,invoiceitem 模型对象属性填充了正确的数据,并且我使用 db.SaveChanges() 调用的唯一数据库更改是“db.InvoiceItems.Add(invoiceitem);”。谁能告诉我它试图访问、写入或更新名为“UserDetails_UserId”的列的位置和原因。也许我在 DropdownListFor 上搞砸了,因为这是与 UserDetails 表交互的唯一部分?

谢谢。

4

1 回答 1

3

You have a one-to-many relationship setup, the column name you are seeing with the underscores in the exception is the relationship default generated. See here for more details.

于 2012-10-28T05:44:44.307 回答