第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.NET Core 2.0 Web API實體框架核心-尋找更優(yōu)雅的解決方案

ASP.NET Core 2.0 Web API實體框架核心-尋找更優(yōu)雅的解決方案

C#
手掌心 2021-04-29 16:16:45
我正在ASP.NET Core 2.0上的Web api項目上工作,其中Entity Framework Core連接到了postgres數(shù)據(jù)庫。以下PUT方法可以正常工作,但我覺得我可能誤會了update方法的使用,而且我還覺得應(yīng)該有一個更優(yōu)雅的解決方案,以一種持有PUT請求(而不是PATCH)的方式更新現(xiàn)有對象)。    // PUT: api/Discount/5    [HttpPut("{id}")]    [ProducesResponseType(204)]    [ProducesResponseType(400)]    [ProducesResponseType(404)]    [ProducesResponseType(500)]    public IActionResult Put(int id, [FromBody]Discount discount)    {        if (ModelState.IsValid)        {            using (var context = new BarberskabetDbContext())            {                if (context.Discounts.Any(x => x.DiscountId == id && !x.Deleted))                {                    var dbDiscount = context.Discounts.FirstOrDefault(x => x.DiscountId == id && !x.Deleted);                    dbDiscount.AppliesOnce = discount.AppliesOnce;                    dbDiscount.AppliesToProductType = discount.AppliesToProductType;                    dbDiscount.Code = discount.Code;                    dbDiscount.DiscountType = discount.DiscountType;                    dbDiscount.Duration = discount.Duration;                    dbDiscount.DurationUsageLimit = discount.DurationUsageLimit;                    dbDiscount.EndsAt = discount.EndsAt;                    dbDiscount.OncePerCustomer = discount.OncePerCustomer;                    dbDiscount.RestrictByEmail = discount.RestrictByEmail;                    dbDiscount.StartsAt = discount.StartsAt;                    dbDiscount.Status = discount.Status;                    dbDiscount.TimesUsed = discount.TimesUsed;                    dbDiscount.UsageLimit = discount.UsageLimit;                    dbDiscount.Value = discount.Value;                    context.Update(dbDiscount);                    if (context.SaveChanges() == 0)                    {                        return StatusCode(500, "Unable to update record.");                    }                    return NoContent();                }我覺得應(yīng)該有更好的方法將新信息放入數(shù)據(jù)庫中,但是我很難看到它。
查看完整描述

1 回答

?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗 獲得超9個贊

Update方法用于所謂的強制更新,即,當(dāng)您確定存在的實體斷開連接時。它可以在不從數(shù)據(jù)庫加載實體的情況下工作,并且只需更新所有屬性即可。


以您的示例為例,強制更新如下所示:


if (context.Discounts.Any(x => x.DiscountId == id && !x.Deleted))

{

? ? context.Update(discount);

? ? context.SaveChanges();

}

如果只想更新已更改的屬性(如果有),則應(yīng)該加載數(shù)據(jù)庫實體并使用該CurrentValues.UpdateValues方法來應(yīng)用更改:


var dbDiscount = context.Discounts.FirstOrDefault(x => x.DiscountId == id && !x.Deleted);

if (dbDiscount != null)

{

? ? context.Entry(dbDiscount).CurrentValues.SetValues(discount);

? ? context.SaveChanges();

}

請注意,在這種情況下,EF可能根本不會發(fā)出更新命令(如果所有屬性值都與原始屬性值相同),即SaveChanges可以返回0,因此您不應(yīng)將其用作成功的指示。實際上,您永遠(yuǎn)不應(yīng)將其用于此目的,因為EF會拋出異常,這是有問題的。



查看完整回答
反對 回復(fù) 2021-05-16
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號