简体   繁体   中英

MVC 4 DbContext Enable-Migrations re-writes seed code in Configuration.cs

I'm trying to do code first in a new MVC 4 project and have a "journal" mvc that works just fine and its Seed function in /Migrations/Configuration.cs working as well. However, when I went to work on the UserProfile model in AccountModels.cs and then use the Enable-Migrations for it I lost all my seed code for the journal model and it was replace with a shell for UserProfile. :(

When using code first and having multiple models, inevitably all with their own DbContext (unless they should all share one?) how can I retain the seed code for each DbContext and model?

I'm shocked no one else has ran into this problem before. I simply don't want Configuration.cs re-written every-time I use the package manager console in a code first mvc project when doing the Enable-Migrations command, or at least why cant each dbcontext have their own configuration.cs, or something! Looks like I may have to offer a bounty soon.

Ok, i figured this out. The trick is to rename the Configuration.cs class after it gets created. Then, later when you add new migrations and do updates you specify the configuration as shown below.

Enable-Migrations -ContextTypeName LDN.Models.Cohort

add-migration -ConfigurationTypeName LDN.Migrations.ConfigurationCohort CohortInitial

update-database -verbose -Configuration LDN.Migrations.ConfigurationCohort

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM