Insert new row to database with computed columns

I'm new in asp mvc. I have one problem, that i could'nt resolve from two days ... I have four computed columns in my database. I want to insert new row to database, of course only variables that aren't computed. But I received errors that I couldn't update computed columns, but I don't want to! I tried to use AutoMapper, but maybe I'm using it wrong. How can I add new row to database? Please help me.

My Model:

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

My Controller:

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

Answers


You must decorate your column with this attribute

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

Need Your Help

iPhone user's mobile number

iphone

Can I get user's mobile number (CLI number) in Objective-C on the iPhone from its sim card?

Updating two tables in a many-to-one relationship through a view in PostgreSQL

sql database postgresql

I have two tables: foos and bars, and there is a many-to-one relationship between them: each foo can have many bars. I also have a view foobars, which joins these two tables (its query is like sele...