Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a property on a model class that's not related to the database?

so i have this model class:

[Table("items")]
public class items
{
        public int id { get; set; }
        public string text { get; set; }
        public string value { get; set; }
   //   public int test { get; set; } crashes
}

I use entity framework. my table has id,text,value and test property is not part of it. I populate my DbSet like this

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

Odd enough, it doesn't crash if i add something like public List<string> test { get; set; }.

What should i do? i originally wanted to add a bool? property.

edit: I was thinking of making a new class that will inherit the model, but i will have to remap/re-populate it.

like image 896
Carlos Miguel Colanta Avatar asked Oct 21 '25 14:10

Carlos Miguel Colanta


1 Answers

You can add the NotMapped attribute.

For example:

[Table("items")]
public class items
{
    public int id { get; set; }
    public string text { get; set; }
    public string value { get; set; }

    [NotMapped]
    public int test { get; set; }
}
like image 173
Mun Avatar answered Oct 23 '25 06:10

Mun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!