[RESOLVED]A little lost with Code Based Migrations

Result 1 of "update-database" command in Package Manager Console:


System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

Response: Changed Entities in Model by removing "?" to change datatype from nulls allowed to not null.

        public DateTime? AdminDateCreate { get; set; }
        public DateTime? AdminDateView { get; set; } 


        public DateTime MediaAdminDateCreate { get; set; }
        public DateTime MediaAdminDateView { get; set; }    

Result 2 of "update-database" command in Package Manager Console:


Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled
to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.


1.   Is my premise wrong?  Isn’t making changes to an entity what code based migration is for:  I had a model from which an [timestamp]InitialCreate.cs migration file is created.  A change (datetime? to datetime) to the entity is made and update-database command
in Package Manager Console should implement the change.  Why is changing the model from datetime? to datetime throwing an error?

Migrations seems to have hiccups from time to time.  When it prevents me from updating a database, I usually just delete the entire Migrations folder, then run the initial commands again.

> enable-migrations
> add-migration InitialCreate

and then any time I make a change, I run the ‘update-database’ command

If additional errors occur, I delete the database.  It will always re-create itself (and if you’ve got a seed file, create data)

Thank you John.

Leave a Reply