so I want to get started with EntityFramework 4.3 migrations. I wanted to know if I can convert an existing database to a migration-enabled database and have EF assume that only changes from then on should be considered migrations.
相关问题
- How to Specify a Compound key in Entity Framework
- ToList method not available for TrackableCollectio
- Getting “Cannot insert the value NULL into column”
- How can I implement Query Interception in a LINQ t
- Setting table level WillCascadeOnDelete not availa
相关文章
- How do we alias a Sql Server instance name used in
- entity type has no key defined - Code first
- POCO Vs Entity Framework Generated Classes? [close
- The 'DbProviderFactories' section can only
- Is SaveChanges() Necessary with Function Imports (
- Entity Framework - An item with the same key has a
- Setup EF4 data source for SQL Compact 4
- Id of newly added Entity before SaveChanges()
So it seems what I was looking for is Codebased Migrations which is activated when I set AutomaticMigrationsEnabled=false. My models were generated from an existing database. To activate migrations, all I needed to do was enable migrations (Enable-Migrations), create a new new migration file using Add-Migration, empty it out (my models are already in the database so I don't want EF to try and create them) and deploy that. To deploy, I added the following to my Global.asax file:
A new table __MigrationHistory was created and a new migration record was create in it. This new migration record had a hash of my models so now any changes to my models can be scripted for me in future migrations with EF.
To test, I created another migration file (Add-Migration), I added a new property to a model, ran Add-Migrations which scripted the new field and then deployed my application. The migration was run as expected.
A nice walk-through for this is posted here: http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database/
The one change I would suggest is to simply comment out the code in the Up and Down methods until you have deployed the migration. After that, you can uncomment the code and that will allow you to create a new database if you need to later.
Add-Migration -IgnoreChanges
See https://msdn.microsoft.com/en-us/data/dn579398.aspx