2011-05-29 44 views

回答

2

您应该可以通过类构造函数设置默认值。例如 - 我有以下数据模型类。我正在使用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; } 
    } 
} 
+0

我知道关于使用构造函数。那么使用MetadataAttribute呢?它是否可行? – Calabonga 2011-06-02 04:08:10

+0

@Calabonga - 不是我所知道的。 AFAIK唯一的方法是通过构造函数。 – 2011-06-02 10:09:44