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

Broken table header in a Bootstrap modal

jquery css twitter-bootstrap datatables

I need to put a scrollable datatable in a modal bootstrap. The problem is that the header is broken . Only solved if I resize the browser or if I click on the header to reorder. I try to put the same

Background running app that shows a dialog when orientation changes

android multithreading dialog orientation-changes background-service

The title is pretty self explanatory. I want to create an android app that is always running and when the orientation is changed it shows a dialog. I was looking for some information I think I have...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.