2011-05-29 47 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