1

I am new to asp.net mvc 5. I just build a new project using asp.net mvc 5 code first. One of my class's field names does not store in the database when I run the application.

Here is my code:

public class Read
    {
        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

        public long id_read { get; set; }
        public long id_install { get; set; }
        public int id_employee { get; set; }
        public int read_result { get; set; }
        public string digit_image { get; set; }   

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime read_date { get; set; }

        [DataType(DataType.Currency)]
        public decimal? price { get; set; }

        [DataType(DataType.Currency)]
        public decimal? total
        {
            get
            {

                return read_result * price;
            }
        }

    }

Since the total field name is the result of multiplication between read_result and price. So, I want to the total field name store in the database. Please help me.

Vosco
  • 51
  • 2
  • 7
  • Show your table structure in database. I am under the impression the `total` field doesn't actually exist. Since this is code first. – AbdulG Jan 25 '20 at 07:49

1 Answers1

0

This is just your Model class. I would suggest to write logic of insertion in separate service class with multiplication logic over there.

//Model Class
public class Read{
public decimal? total{get;set;}
}

//Service Class
var read = new Read()
read.read_result = 10;
read.price = 10;
read.Total = read_result * price;
context.Add(read);
Pranay
  • 432
  • 4
  • 14