Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Default value for property using Data Annotations

I have a model like this

public class MyModel
{
    public int MyModelId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
}

I was wondering if there's a way, using Data Annotations, to set the value of a property - say Title - default to other property value, i.e. Name. Something like:

if(MyModel.Title == "") MyModel.Title = MyModel.Name;
like image 381
noinstance Avatar asked Jun 08 '11 01:06

noinstance


1 Answers

If you want default value set it in entity default (parameterless) constructor. There is no need to have data annotation for something which you can do directly.

like image 198
Ladislav Mrnka Avatar answered Sep 28 '22 12:09

Ladislav Mrnka