I have redesigned my class scheme many times since started the project I'm working on.
At first I didn't know about data annotations, so I have migration1
, where my Ids have no DatabaseGenerated.Identity
option and migration4
where they already have it.
Turns out that EF doesn't work this way ( https://entityframework.codeplex.com/workitem/509 as for EF5), so when I added some seed data, it threw an exception
Cannot insert the value NULL into column 'primarykeycolumn', table 'tablename'; column does not allow nulls. INSERT fails.
What's really interesting is when I deleted all existing migrations and scaffolded new one from scratch, seed method ran without any problems.
So I have a question: do I need to do it any time I make changes to scheme such as adding data annotations like Identity
or is there a way to save my previous migrations? Because dropping and recreating db in real-life can result in a huge data loss, which I want to avoid.
Adding Migrations. In your Package Manager Console
Add-Migration Test1
In your Configuration.cs from Migration Class You will see Up method and Down Method. It is like Before, and After Changes. You can call them up if you want to reverse the Changes.
doing Add-Migration "NameoftheMigration" does not implement it
you need to call
Update-Database
If it tells you that there is an error, or potential Data lost.
Update-Database -Force
If you choose to use CodeFirst. You can't really Jump from Altering table by Design, then by code. You have to Choose one. As CodeFirst Migration rely on Models. Data First, Rely on Data Design.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.