EF6:使用代码优先迁移重命名命名空间 [英] EF6: Renaming namespace using Code First Migrations

查看:471
本文介绍了EF6:使用代码优先迁移重命名命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以重命名整个项目的命名空间(包括当然包括:DbContext类,迁移配置类等),而不会破坏任何东西或必须重新创建我的所有迁移?

It is possible for me to rename the namespace of my entire Project (including of course: DbContext class, Migrations configuration classes, etc) without breaking anything or having to recreate all my migrations?

说,我有Project MyProject,命名空间是

Say, I have Project MyProject, which namespace is

Foo.MyProject

我的配置类在

Foo.MyProject.Migrations

说我想重命名 Foo 命名空间对于 Bar ,当然我的配置命名空间现在将是

Say I want to rename Foo namespace for Bar, and of course my Configurations namespace now will be

Bar.MyProject.Configurations

是否有任何正确的方法,并保持我目前的所有迁移仍然有效?
这些方法是否需要手动编辑___MigrationHistory表? (一目了然,我看到ContextKey列,我怀疑我应该手动编辑。)

Is there any correct way of doing this and maintain all my current Migrations still working? Do these ways involve manually editing the ___MigrationHistory table or something? (at a glance I see the ContextKey column, which I suspect I should manually edit.)

推荐答案

是的,你确实需要更新_MigrationHistory表中的ContextKey。代码:

Yes, you do indeed need to update the ContextKey in the__MigrationHistory table. Code:

UPDATE [dbo].[__MigrationHistory] 
   SET [ContextKey] = 'New_Namespace.Migrations.Configuration'
 WHERE [ContextKey] = 'Old_Namespace.Migrations.Configuration'

使用EF6重命名名称空间的主题:

A good read on the topic of renaming namespaces with EF6:

http://jameschambers.com/2014/02/changing-the-namespace-with-entity-framework-6-0-code-first-databases/

这篇关于EF6:使用代码优先迁移重命名命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆