I don't seem to figure it out how to use enum values in an ASP.NET MVC 3 model (using code first approach) so that they are stored in a database and are preserved.
My model code looks like:
public class TestNews
{
public int Id { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
//[EnumDataType(typeof(TestNewsType))]?
public TestNewsType Type { get; set; }
[Required]
public string Title { get; set; }
}
public enum TestNewsType : short
{
Draft = 0,
PublishedPublicly = 1,
PublishedInternally = 2,
Removed = 3
}
After the database gets recreated it contains the table TestNews, but it contains only the columns:
- Id
- Date
- Title
How to make it also store the Type in the database? And also should the EnumDataType annotation be used?
I would later like to use it in a controller action like:
public ActionResult Latest(int count=5)
{
var model = db.TestNews
.Where(n => n.Type == TestNewsType.PublishedPublicly)
.OrderByDescending(n => n.Date)
.Take(count)
.ToList();
return PartialView(model);
}