2012-12-09 64 views
0

我建立一個mvc應用程序,首先使用EF代碼。我正準備開始。這裏是與我的問題有關的代碼。更新數據庫的第一次,得到錯誤

廣告模特

[Key] 
    public virtual int ID { get; set; } 
    public virtual int UserID { get; set; } 
    public virtual int Cycles { get; set; } 
    public virtual decimal Cost { get; set; } 
    public string Name { get; set; } 
    public virtual List<AdDisplayTime> DispalyTime { get; set; } 
    public virtual DateTime CreateDate { get; set; } 
    public virtual DateTime UpdateDate { get; set; } 
    public virtual int UpdatedBy { get; set; } 
    public virtual bool FullScreen { get; set; } 

AdDisplayTimes型號

[Key] 
public virtual int ID { get; set; } 
public virtual int AdID { get; set; } 
public virtual string DisplayTime { get; set; } 

狀態模式

[Key] 
public virtual int ID { get; set; } 
public virtual string Name { get; set; } 

這裏在我的移民類種子法

var adDisplayTimes = new List<AdDisplayTime> 
           { 
            new AdDisplayTime{AdID = 1,DisplayTime = "10:30"}, 
            new AdDisplayTime{AdID = 1,DisplayTime = "10:40"}, 
            new AdDisplayTime{AdID = 1,DisplayTime = "10:50"}, 
            new AdDisplayTime{AdID = 1,DisplayTime = "11:00"} 
           }; 
     context.Advertisements.AddOrUpdate(a => a.Name, 
      new Advertisement 
       { 
        Name = "Fisrt Ad", 
        OwnerID = 1, 
        Cycles = 125, 
        Cost = (decimal)234.45, 
        FileName = "test.jpg", 
        Tier = 1, 
        Path = "~/UploadedImages/Pending/test.jpg", 
        DispalyTime = adDisplayTimes, 
        CreateDate = DateTime.Now, 
        ValidFrom = DateTime.Now, 
        ValidTill = DateTime.Now.AddMonths(1), 

        FullScreen = true, 
        IsAdminAd = false, 
        Status = 1 
       } 
      ); 
     context.AdDisplayTimes.AddOrUpdate(ad=>ad.AdID, 
            new AdDisplayTime { AdID = 1, DisplayTime = "10:30" }, 
            new AdDisplayTime { AdID = 1, DisplayTime = "10:40" }, 
            new AdDisplayTime { AdID = 1, DisplayTime = "10:50" }, 
            new AdDisplayTime { AdID = 1, DisplayTime = "11:00" } 
      ); 
     context.Statuses.AddOrUpdate(s=>s.Name, 
      new Status{Name = "Pending"}, 
      new Status{Name = "Approved"}, 
      new Status{Name = "Edited"}, 
      new Status{Name = "Activated"}, 
      new Status{Name = "DeActivated"} 
      ); 

當我運行從Visual Studio程序包管理器CONSOL命令「更新數據庫」我得到 這個

update-database 
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. 
No pending code-based migrations. 
Running Seed method. 
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 
The statement has been terminated. 
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 
at System.Data.SqlClient.SqlDataReader.get_MetaData() 
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) 
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) 
--- End of inner exception stack trace --- 
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) 
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) 
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) 
at System.Data.Entity.Internal.InternalContext.SaveChanges() 
--- End of inner exception stack trace --- 
at System.Data.Entity.Internal.InternalContext.SaveChanges() 
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 
at System.Data.Entity.DbContext.SaveChanges() 
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase() 
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() 
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) 
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) 
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) 
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) 
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 
An error occurred while updating the entries. See the inner exception for details. 

請幫我解決這個

回答

3

如果你檢查內部異常,你可以看到這個錯誤

The conversion of a datetime2 data type to a datetime data 
type resulted in an out-of-range value 

和SO被定義DateTime列的數據類型爲datetime2.you可以用數據詮釋喜歡做它找到共同的解決辦法:

[Column(TypeName="datetime2")] 

,並與流暢API,如:

HasColumnType("datetime2") 

希望這會有所幫助。