Sunday 15 October 2017

c# - Entity Framework SaveChangesAsync Not Updating Database

Invoice Category
Entity:



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

[Required]


public string CategoryName { get; set;
}
}


Invoice
Item Entity



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; }


}


Invoice
Category Edit
View



@model
Accounts.Models.InvoiceCategory

Settings



Invoice
Categories





asp-action="Edit">
class="text-danger">


@(Html.Kendo().Grid()

.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 #" +

""
);

columns.Bound(p =>
p.CategoryName).ClientTemplate("#= CategoryName #" +
" name='[#= index(data)#].CategoryName' value='#= CategoryName #'/>"

);
columns.Template("
").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()
)





@section Scripts {

@Html.Kendo().DeferredScripts()




}



Controller
Invoice Category Update
Method:



[HttpPost]
[ValidateAntiForgeryToken]
public
async Task Edit(IEnumerable
invoiceCategory)
{
if (ModelState.IsValid)
{

try

{

_context.InvoiceCategory.UpdateRange(invoiceCategory);

_context.SaveChangesAsync();
}
catch (Exception ex)

{
...
}

}
}



The
values are being changed in the view and the changes are being sent to the Edit method
of the controller however the changes are not promulgated to the database for some
reason with the save method.



Can Entity
framework not support a lookup table with no integer identity column like I have? Or am
I doing something stupid?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...