簡體   English   中英

使用計算列將新行插入數據庫

[英]Insert new row to database with computed columns

我是asp mvc的新手。 我有一個問題,我兩天都無法解決...我的數據庫中有四個計算列。 我想向數據庫插入新行,當然只有未計算的變量。 但我收到錯誤,我無法更新計算列,但我不想! 我試圖使用AutoMapper,但也許我使用它錯了。 如何向數據庫添加新行? 請幫我。

我的型號:

namespace Aplikacja_v0.Models

public class OtwarteContext : DbContext
{
    public OtwarteContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Otwarte> Otwarte { get; set; }
}


[Table("OtwartePozycje")]
public partial class Otwarte
{
    [Key]
    public int OtwartaPozycjaID { get; set; }
    public int UserID { get; set; }
    public string Nazwa { get; set; }
    public int Ilosc { get; set; }
    public decimal KursNabycia { get; set; }
    public int Stan { get; set; }
    public decimal KursAktualny { get; set; }
    public decimal KursZamkniecia { get; set; }
    public decimal WartoscPortfela { get; set; }

    //Computed columns
    public decimal WartoscNabycia { get; private set; }
    public decimal WartoscAktualna { get; private set; }
    public decimal ZmianaProcentowa { get; private set; }
    public decimal ZmianaKwotowa { get; private set; }
}

我的控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Otwarte otwarte)
{
    if (ModelState.IsValid)
    {
        var userIDD = WebSecurity.GetUserId(User.Identity.Name);
        OtwarteViewModel dbcontext = new OtwarteViewModel();

        dbcontext.Nazwa = otwarte.Nazwa;
        dbcontext.Ilosc = otwarte.Ilosc;
        dbcontext.KursAktualny = otwarte.KursAktualny;
        dbcontext.KursNabycia = otwarte.KursNabycia;
        dbcontext.Stan = 1;
        dbcontext.UserID = WebSecurity.GetUserId(User.Identity.Name);
        dbcontext.WartoscPortfela = 0;
        dbcontext.KursZamkniecia = 0;

        Mapper.CreateMap<OtwarteViewModel, Otwarte>();
        var mapped = Mapper.Map<OtwarteViewModel, Otwarte>(dbcontext);

        db.Otwarte.Add(mapped);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(otwarte);
}

您必須使用此屬性修飾列

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM