1
如何为我的“仅限代码”类(传递给sdf或mdf)设置默认值? MVC3版本是否支持此功能?MVC3:在“仅限代码”中设置默认值
如何为我的“仅限代码”类(传递给sdf或mdf)设置默认值? MVC3版本是否支持此功能?MVC3:在“仅限代码”中设置默认值
您应该可以通过类构造函数设置默认值。例如 - 我有以下数据模型类。我正在使用MVC3和实体框架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; }
}
}
我知道关于使用构造函数。那么使用MetadataAttribute呢?它是否可行? – Calabonga 2011-06-02 04:08:10
@Calabonga - 不是我所知道的。 AFAIK唯一的方法是通过构造函数。 – 2011-06-02 10:09:44