如何在代码中设置默认值First Entity Framework

时间:2015-07-06 15:37:48

标签: c# entity-framework ef-code-first

我在我的实体类中为CreatedDate设置默认值,如下所示。但在实施过程中,我得到了值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; }
}    

这是我的实现

  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;
            }
        }

0 个答案:

没有答案