1
發票分類實體:實體框架SaveChangesAsync不更新數據庫
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; }
}
發票分類,編輯,查看
@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>
}
控制器發票類別更新方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategory)
{
if (ModelState.IsValid)
{
try
{
_context.InvoiceCategory.UpdateRange(invoiceCategory);
_context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
}
的值在視圖中被改變和變化被髮送到然而的更改不會頒佈給數據庫出於某種原因控制器的編輯方法用保存方法。
實體框架不支持像我這樣沒有整數標識列的查找表嗎?還是我在做一些愚蠢的事情?
+1感謝克里斯託。所以我明白我想要做什麼是不可能的,我需要添加一個int標識列到查找表? – Reafidy
所以我已經添加了一個標識列到查找表(InvoiceCategoryID),仍然沒有更新數據庫。我不確定我是否正確使用updaterange。 – Reafidy
@Reafidy不用客氣。你可以請你的數據庫的架構圖(只有表相關'InvoiceCategory')?此外,如果你能描述一個用例,這將會非常有幫助,以便理解你擁有什麼以及你想實現什麼。提前致謝 ! – Christos