Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

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.

Upvotes: 4

Views: 1478

Answers (1)

Mun
Mun

Reputation: 14308

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

Upvotes: 5

Related Questions