2012-11-18 18 views
2

我有一個DateTime字段,我想設置爲一個默認值,每當一個記錄被輸入到該表(它基本上是一個DateCreated字段)。這是我的代碼代碼首先默認的日期時間不工作

public partial class User 
    { 
     public User() 
     { 
      this.DateCreated = DateTime.Now; //set default value 
      Roles = new HashSet<Role>(); 
     } 

     public ICollection<Role> Roles { get; set; } //many to many relationship 

     public int UserId { get; set; } 
     public string FirstName { get; set; } 
     public string Surname { get; set; } 
     public string Username { get; set; } 
     public string Password { get; set; } 
     public string City { get; set; } 

     //foreign key 
     public int CountryId { get; set; } 
     //navigation properties 
     public virtual Country Country { get; set; } 

     //foreign key 
     public int LanguageId { get; set; } 
     //navigation properties 
     public virtual Language Language { get; set; } 

     public string EmailAddress { get; set; } 
     public long FacebookId { get; set; } 
     public DateTime DateCreated { get; set; } 
    } } 

    public class UserConfiguration : EntityTypeConfiguration<User> 
    { 
     public UserConfiguration() 
     { 
      ToTable("Users", schemaName: "Main"); 

      HasKey(s => s.UserId); 

      Property(p => p.FirstName) 
       .IsRequired().HasMaxLength(50); //translates to non-nullable  

      Property(p => p.Surname) 
       .IsRequired().HasMaxLength(50); 

      Property(p => p.Username) 
       .IsRequired().HasMaxLength(20); 

      Property(p => p.Password) 
       .IsRequired().HasMaxLength(20); 

      Property(p => p.City) 
       .IsOptional().HasMaxLength(50); //not required 

      Property(p => p.LanguageId) 
       .IsRequired(); 

      Property(p => p.EmailAddress) 
       .IsRequired().HasMaxLength(50); 

      Property(p => p.FacebookId) 
       .IsOptional(); 

      Property(p => p.DateCreated) 
       .IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
      //attribute ensures that DateCreated only gets saved to the database when you are adding a 
      //row, not when you are saving 
     } 
    } 

    context.Users.Add(new User 
       { 
        FirstName = "Test", 
        Surname = "User", 
        Username = "testuser72", 
        Password = "testuser72", 
        City = "London", 
        CountryId = context.Countries.Single(d => d.CountryName == "United Kingdom").CountryId, 
        LanguageId = context.Languages.Single(d => d.LanguageName == "English").LanguageId, 
        EmailAddress = "[email protected]", 
        Roles = new Role[] { context.Roles.Single(r => r.RoleName == "Content Editor"), context.Roles.Single(s => s.RoleName == "User") } 
       }); 

爲什麼當我嘗試使用上面爲什麼會出現錯誤的代碼添加用戶「不能將NULL值插入列‘dateCreated會’,表‘Main.Users’; 列不允許有空值。 INSERT失敗。'?

回答

4

使用「HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)」選項,您說您希望它是一個自動增加列。這不是你想要的。你需要Computed,它表示該值是由數據庫自動填充的,而不是由你自己填充。

只是更改此選項並不能解決您的問題。通過將它設置爲Computed,你說你不希望它取自己的值,所以在構造函數中設置它是毫無意義的。您必須在數據庫級別將缺省值設置爲「GETDATE()」。在使用遷移時,將字符串添加到defaultValueSql參數。

+0

因此,在初始數據庫創建和播種時,我會留下這些DateTime字段,然後在使用遷移時稍後添加它們? – user517406

+0

不,我用普通的SQL創建數據庫的時候,然後這樣做:「add CreatedDate datetime2 default GETDATE()」。當您使用遷移創建表時,您可以修改遷移併爲您的列傳遞「defaultValueSql」參數。 – fdghdfhdfgh

-1

一旦你通過運行Add-Migration 20170303_example產生遷移到20170303_example文件,然後

查找例如DateTime列:

CreatedDate = c.DateTime(nullable: false) 

它更新到:

CreatedDate = c.DateTime(defaultValueSql: "GETDATE()") 

最後運行Update-Database命令在您的軟件包管理器控制檯中更新數據庫。