0

I have the following object declared in C# ASP.NET MVC, which represents an object in our Azure SQL Server database.

public class Note
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    [Display(Name = "Last Edited")]
    [HiddenInput(DisplayValue = true)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime LastEditDateUTC { get; set; }

    [StringLength(300)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required(AllowEmptyStrings = true)]
    public String Subject { get; set; }

    [StringLength(1000)]
    [DataType(DataType.MultilineText)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    [Required(AllowEmptyStrings = true)]
    public String Text { get; set; }
}

This is how I would save this object to the SQL database:

Note note = new Note()
{
    // Fill out all the variables
};

try
{
    mySQLServer.mySQLTable.Add(note);
    mySQLServer.SaveChanges();
}
catch (Exception e)
{
    // Log
}

What I would like is to add one more variable to Note named IsSelected, but I would not like this variable to be saved to the SQL server's table when the mySQLServer.SaveChanges() method is called, because the IsSelected variable would be used with the UI only. Is there a Data Annotation property that can be added to IsSelected to accomplish this? Is this use case even possible?

aBlaze
  • 2,436
  • 2
  • 31
  • 63
  • 1
    Use a view model - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  May 21 '18 at 23:58
  • Ideally you should use a view model. But if you decide to not go that approach, you can add the `[NotMapped]` attribute. See this [Ignoring a class property in Entity Framework](https://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first/10385738#10385738) – Shyju May 21 '18 at 23:59

0 Answers0