简体   繁体   English

如何使用代码优先实体框架设置默认日期

[英]How do I set a default date using code first entity framework

I am trying to set a default value for the last date (DateAdded) property using Entity Framework with code first methods. 我正在尝试使用带有代码优先方法的Entity Framework为最后一个日期(DateAdded)属性设置默认值。 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; }

    }
}

You could set it up in the constructor: 您可以在构造函数中进行设置:

public class UserProfile()
{
   DateAdded = DateTime.Now;
}

i used to handle this like in te following link: 我以前在以下链接中处理过此问题:

setting default values 设置默认值

basically using a "factory" with reflection. 基本上使用带有反射的“工厂”。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Entity Framework 6 Code First 设置默认值约束? - How can set a default value constraint with Entity Framework 6 Code First? 在Code First Entity Framework Fluent API中的模型生成过程中设置默认的Date值 - Set default Date value during Model Generation in Code First Entity Framework Fluent API 如何从SQL Server中填充一组实体框架代码优先POCOS(一次关闭)? - How do I populate a set of Entity Framework Code first POCOS from SQL server (once off)? 如何在Entity Framework Code First中加密数据? - How do I encrypt data in Entity Framework Code First? 如何使用实体框架代码优先将 Boolean 数组绑定到 C# model? - How do I bind a Boolean array to a C# model using Entity Framework Code First? 如何使用C#-WPF-Entity-Framework Code First应用程序创建数据库备份? - How do I create database backups using C#-WPF-Entity-Framework Code First application? 实体框架6:如何使用代码优先迁移和FTP部署重置数据库 - Entity Framework 6: How do I reset a database using code first migrations and an FTP deploy 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first? 如何首先使用实体​​框架代码指定字段的最大列长度 - How do I specify the maximum column length of a field using entity framework code first 如何在 MVC3 和实体框架上使用 Code First 将另一个表添加到我的数据库中? - How do I add another table to my database using Code First on MVC3 and Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM