MVC3:在“仅代码”中设置默认值

时间:2011-05-29 23:17:25

标签: asp.net-mvc-3 code-first default-value

如何为“Code-Only”类设置默认值(传递给sdf或mdf)? MVC3版本是否支持此功能?

1 个答案:

答案 0 :(得分:2)

您应该能够通过类构造函数设置默认值。例如 - 我有以下数据模型类。我正在使用MVC3和Entity Framework 4.1。

namespace MyProj.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Task
    {
        public Task()
        {
            this.HoursEstimated = 0;
            this.HoursUsed = 0;
            this.Status = "To Start";
        }

        public int ID { get; set; }
        public string Description { get; set; }
        public int AssignedUserID { get; set; }
        public int JobID { get; set; }
        public Nullable<decimal> HoursEstimated { get; set; }
        public Nullable<decimal> HoursUsed { get; set; }
        public Nullable<System.DateTime> DateStart { get; set; }
        public Nullable<System.DateTime> DateDue { get; set; }
        public string Status { get; set; }

        public virtual Job Job { get; set; }
        public virtual User AssignedUser { get; set; }
    }
}