gpt4 book ai didi

c# - Entity Framework SaveChangesAsync 不更新数据库

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:01 27 4
gpt4 key购买 nike

发票类别实体:

public class InvoiceCategory
{
[Key]
Public int InvoiceCategoryID { get; set; }

[Required]
public string CategoryName { get; set; }
}

发票项目实体

public class InvoiceItem
{
[Key]
public int InvoiceItemID { get; set; }

[Required]
public int InvoiceID { get; set; }

[Required]
[Display(Name = "Date Completed")]
[DataType(DataType.Date)]
public DateTime? Date { get; set; }

[StringLength(50)]
public string InvoiceCategory { get; set; }

[Required]
[StringLength(200)]
public string Description { get; set; }

[Required]
[StringLength(20)]
public string Unit { get; set; }

[Required]
[DataType(DataType.Currency)]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]
public decimal Price { get; set; }

[Required]
[DefaultValue(1)]
public decimal? Quantity { get; set; }

public virtual Invoice Invoice { get; set; }
}

发票类别编辑 View

@model Accounts.Models.InvoiceCategory
<h2>Settings</h2>
<br />
<h4>Invoice Categories</h4>
<hr />
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
@(Html.Kendo().Grid<Accounts.Models.InvoiceCategory>()
.Name("InvoiceCategory")
.ToolBar(tools =>
{
tools.Create().Text("New Category");
})
.Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom).DisplayDeleteConfirmation(false))
.Columns(columns =>
{
columns.Bound(p => p.InvoiceCategoryID).Hidden().ClientTemplate("#= InvoiceCategoryID #" +
"<input type='hidden' name='[#= index(data)#].InvoiceCategoryID' value='#= InvoiceCategoryID #'/>"
);

columns.Bound(p => p.CategoryName).ClientTemplate("#= CategoryName #" +
"<input type='hidden' name='[#= index(data)#].CategoryName' value='#= CategoryName #'/>"
);
columns.Template("<a class='btn btn-danger btn-sm k-grid-delete'><span class='glyphicon glyphicon-trash'></span> </a>").Width(50);
})
.DataSource(dataSource => dataSource.Ajax()
.Model(model =>
{
model.Id(p => p.InvoiceCategoryID);
model.Field(p => p.InvoiceCategoryID).Editable(false);
})
.Read(read => read.Action("InvoiceCategories_Read", "Settings"))
.ServerOperation(false)
).Deferred()
)
<br />
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-floppy-disk" style="vertical-align:middle;margin-top:-5px"></span> Save</button>
</form>
@section Scripts {
@Html.Kendo().DeferredScripts()

<script>
function index(dataItem) {
var data = $("#InvoiceCategory").data("kendoGrid").dataSource.data();
return data.indexOf(dataItem);
}
</script>

<style>
.k-grid {
width: 280px;
}
</style>
}

Controller 发票类别更新方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategory)
{
if (ModelState.IsValid)
{
try
{
_context.InvoiceCategory.UpdateRange(invoiceCategory);
_context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
}

值在 View 中被更改,更改被发送到 Controller 的 Edit 方法,但是由于某些原因,更改没有通过 save 方法发布到数据库。

Entity Framework 能否像我一样不支持没有整数标识列的查找表?还是我在做傻事?

最佳答案

本质上,您尝试做的是更新名为 InvoiceCategory 的表的主键。无论如何,对于代码的上下文,如果您在数据库的上下文中考虑它,这是没有意义的。主键是记录的唯一标识符。您应该无法更新它。

作为旁注,无论何时使用 async,您都应该至少使用一个 use await。否则你的方法将被同步执行。在您的情况下,缺少的 await 是这样的:

await _context.SaveChangesAsync();

更新

我认为您需要的是以下内容:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategories)
{
if (ModelState.IsValid)
{
try
{
foreach(var invoiceCategory in invoiceCategories)
{
if(invoiceCategory.InvoiceCategoryID == 0)
{
_context.InvoiceCategory.Add(invoiceCategory);
}
else
{
_context.InvoiceCategory.Update(invoiceCategory);
}
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
}

关于c# - Entity Framework SaveChangesAsync 不更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46803282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com