在做數據庫規划時,通常會規划一些系統字段,也就是由數據庫本身自行指定默認值到這個字段上,創建新的“創建時間(CreateDate)”字段就會常常這樣設計。 如果希望能有默認值,且讓.net 程序在新增信息到數據庫時不用指定的值的話,那么你需要在你的該屬性(property)上面加上一個DatabaseGenerated屬性(Attribute),並傳入DatabbaseGeneratedOption.Computed參數到DatabaseGenerated 屬性中。 在調用DatabaseGenerated屬性時記得提前引用命名空間 System.ComponentModel.DataAnnotations, System.ComponentModel.DataAnnotations.Schema; 具體實現如下: using System; using System.Collections.Generic; using System.Linq; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Artech.VM.Models { public class MovieInfo { [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime? CreateDate { get; set; } } } 當然當你模型生成成功之后你會看到數據庫是沒有設置默認值的,因為EF的ORM是不支持設置默認值的,要設置的話,也只能手動添加上去,EF今后應該也不會添加此功能,因為為了減少.net 原生對象與數據庫數據之間轉換的變量,當模型的數據來自於數據庫自動生成數據時,就很有可能會導致.net對象狀態無法追蹤的情況。所以在使用Entity FrameWrok時候是無法指定數據庫中的默認值的。