Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have model contain field without adding it to the database? [closed]

In my Asp.NET MVC4 application I want to add a field to my model without adding it to the database.

Sort of a field that only lives in c# instances of the model but not in the database itself.

Is there any annotation or other way to exclude the field from the database itself?

I mean an object I can read and write in runtime which the database has no knowledge of.

like image 639
Zebedee Avatar asked Aug 13 '13 17:08

Zebedee


1 Answers

Just decorate your field/property with [NotMapped].

For example:

public class MyModel {     public int Id { get; set; }     public string Name { get; set; }      [NotMapped]     public string Type { get; set; } } 

See http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute.aspx

like image 175
haim770 Avatar answered Oct 02 '22 10:10

haim770