Code first create tables

自闭症网瘾萝莉.ら 提交于 2019-11-30 04:07:25
NSGaga

I'm just going to throw everything in here as a walk-through for getting your code-first and migrations going - attacking various issues that may or may not happen. Note: code-first has proved to be very reliable - and can be worked out with live-scenarios as well - you just need to know what you're doing.

Most likely, your migration and the database are basically out-of-sync.

Your existing migration tries to run against the old copy of the database - with ups/downs that were optimized for the 'empty db' I guess.

You need to run your migrations (Add-Migration) against the copy of the Db that you're attaching to - that'll create a 'difference' and just up-to-date your Db (always make sure to backup of course, as it might drop/change etc.).

Or if possible, empty your Db - and then start fresh.

Or if live Db - create migrations - and run Update-Database -Script on your dev-machine - to generate the full Db - or the changes (this is all very rough, you need to adjust to your case). And then apply to your 'live Db' by running scripts.

You can also check my earlier post on the synchronization...

MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created"

EDIT:
Also check this answer AutomaticMigrationsEnabled false or true?

And if it's ok with your way of doing things add the AutomaticMigrationsEnabled = true; to you Configuration (also under the Migrations folder - created for you)..

Couple steps I always do to clean migrations:

(good to backup first)
- Enable-Migrations -force (first-time only - this deletes the configuration.cs and any 'seed'-ing there!)
- AutomaticMigrationsEnabled = true;
- remove existing migrations by hand from project (\Migrations)
- rebuild the project at this point
- Add-Migration Initial
- Update-Database -Force -Verbose

...later on (subsequent migrations):
- Add-Migration SomeOther1
- Update-Database -Force -Verbose
...providing you keep your Db in sync - i.e. careful with 'moving Db around', manually adjusting etc. (and in that case see the other post)

Couple other things:

With PM Console...
- select your project from the list,
- make sure your 'main exe' (calling your data project / assembly - or that same project if console/app) - is set as 'startup' project,
- always watch the console comments - as it may be trying to build and access different project.

Connection:

See this post of mine Migration does not alter my table
In short - your connection is named like your context + your project - if not specified otherwise. It draws from your DbConfig constructor - or app.config (connections). Make sure you're looking at the 'right database' (i.e. what you look through some explorer and what code-first connects to - may be two different things).

Building:
Make sure your project is set in 'configuration' to build automatically - that can be an issue too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!