0

I am setting up default value for CreatedDate in my entity class as shown below. But during implementation I get the value 01/01/0001

private DateTime _createdDate =  DateTime.Now;
private DateTime _modifiedDate = DateTime.Now;

public DateTime CreatedDate
{
    get { return _createdDate; }
    set { _createdDate = value; }
}

public string LastModifiedBy { get; set; }

public DateTime LastModifiedDate
{
    get { return _modifiedDate; }
    set { _modifiedDate = value; }
}    

here is my implementation

  public bool AddJewelryItem(List<Entities.Jewelry> jewelryList)
    {
        bool isSuccess = false;

        try
        {
            using (var db = new Context())
            {
                foreach (Entities.Jewelry jewelry in jewelryList)
                {


                    db.JewelryItemsList.Add(jewelry.jewelryItems);
                    db.SaveChanges();
                    jewelry.jewelryAddnlDetails.JewelryID = jewelry.jewelryItems.JewelryID;

                    if (jewelry.jewelryAddnlDetails.JewelryID != 0)
                    {
                        db.JewelryAddnlDetailsList.Add(jewelry.jewelryAddnlDetails);
                        db.SaveChanges();
                    }
                }
                isSuccess = true;
            }
        }
nimish jain
  • 141
  • 1
  • 13
  • 1
    if you load data from the db, no matter your init value, the value will be the one in the database. – tschmit007 Jul 06 '15 at 15:49
  • Set the values in the constructor. http://stackoverflow.com/questions/6779881/annotating-properties-on-a-model-with-default-values/6780463#6780463 – Steve Greene Jul 06 '15 at 15:50
  • Unless, at some point in the code you are not showing us, you set NameOfYourObject.CreatedDate = new DateTime(). This would set the date to the default date for a newly created DateTime object which, as it happens, is 01/01/0001. – Kevin Jul 06 '15 at 15:52
  • Like @Kevin said, the code is ok. You can also move the values in the constructor but that will not solve the problem. Insert a breakpoint on CreatedDate setter and see who's changing it. – bubi Jul 06 '15 at 17:30

0 Answers0