1

I am trying to set a default value for the last date (DateAdded) property using Entity Framework with code first methods. Here is my code:

namespace BackOffice.Models
{
    public class UsersContext : DbContext
    {
        public UsersContext()
            //: base("DefaultConnection")
            : base("ProofPixDB")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
    }

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }


        //public DateTime DOB { get; set; }
        [DataType(DataType.Date)]
        public DateTime? DOB { get; set; } //This allows null

        [Required]
        [DataType(DataType.Date)]
        public DateTime DateAdded { get; set; }

    }
}
madhead
  • 31,729
  • 16
  • 153
  • 201
Julian Dormon
  • 1,767
  • 4
  • 32
  • 58
  • Check [this answer](http://stackoverflow.com/questions/7641552/overriding-savechanges-and-setting-modifieddate-but-how-do-i-set-modifiedby/7642041#7642041) – Eranga Sep 01 '12 at 09:57
  • See this http://stackoverflow.com/questions/3137738/how-to-set-default-value-for-pocos-in-ef-cf – Andy Dec 21 '12 at 00:39

3 Answers3

4

You could set it up in the constructor:

public class UserProfile()
{
   DateAdded = DateTime.Now;
}
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
1

i used to handle this like in te following link:

setting default values

basically using a "factory" with reflection.

tschmit007
  • 7,559
  • 2
  • 35
  • 43
  • This looks promising, though when I read your blog post, I was not sure where I would actually call your function. Do you have a sample I could review. – Julian Dormon Sep 05 '12 at 12:28
  • well, for example, anywhere you need a new instance of your type: UserProfil up = EntityTools.GetNewEntity(); will produce a new initialized instance of UserProfil. – tschmit007 Sep 05 '12 at 12:36
  • Thanks so much! Sorry for the delay. I ended up adding this in the controller on postback. – Julian Dormon Sep 26 '12 at 23:58
0

I ended up setting this on postback, like so:

// POST: /Account/Register

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                DateTime rightNow = DateTime.Now;

                //WebSecurity.CreateUserAndAccount(model.UserName, model.Password); //too limited used CreateUserAndAccount method below
                var extendedUserProperties = new { SubscriberId = subscriber.SubscriberId, Email = model.Email, DateAdded = rightNow };
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, extendedUserProperties);
                Roles.AddUserToRole(model.UserName, Request.Form["RoleName"]);
                WebSecurity.Login(model.UserName, model.Password);

                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
Julian Dormon
  • 1,767
  • 4
  • 32
  • 58