如何在exisitng数据库上修复EF代码的第一种方法。 [英] How to fix EF code first approach on exisitng database.

查看:90
本文介绍了如何在exisitng数据库上修复EF代码的第一种方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用EF6创建了两个单独的C#Web应用程序(一个使用Database First,另一个使用Code First方法)。一切都顺利进行,但有一段时间后需要合并两者。

合并后,Code First与Code First Approach相同。

初始我使用'MigrateDatabasetoLatestVersion'代替数据库初始化程序。

所以当我尝试运行我的应用程序时,它抛出与Table相关的错误已经存在。我还尝试使用程序包管理器控制台来更新迁移,它会抛出同样的错误。

所以我无法在同一个数据库上运行新的解决方案。



为解决此问题,我已将数据库初始化程序更改为CreateDatabaseifNotExist。之后,我可以将这个数据库与我的新解决方案一起使用。



但问题出现了,当我尝试向现有表添加新列或创建采用Code First方法的新表。由于没有迁移,因此它会跳过更改/添加表格,为此我必须手动运行Alter / Add表格脚本以进行每次更改。



是否有效这个解决方案?

MigrateDatabasetoLatestVersion不会抛出异常,或者每次都不需要运行db脚本。



谢谢。

Hi,
I have a created two separate C# web application using EF6 (one with Database First and other with Code First approach). everything is going on smoothly but after sometime it is required to merge both.
After merging , Database is same for Code First Approach.
Initial I was using 'MigrateDatabasetoLatestVersion' for database initializer.
So when I try to run my application , it throws error related to Table is already exist. I have also tried with Package manager console to update migration and it throws same error.
and so I was not able to run the new solution on same database.

For resolving this issue, I have changed database initializer to 'CreateDatabaseifNotExist'. and after that I am able to use this database with my new solutions.

But the Problem arise, when I tried to add a new column to existing table or Creating a new table with Code First approach. Since there is no migration so it skips to alter/add table and for that I have to manually run Alter/Add table Script with each changes.

Is there any valid solution for this?
either MigrateDatabasetoLatestVersion doesn't throw exception or it is not required to run db script each time.

Thanks.

推荐答案

您也可以在这里查看:使用ASP.NET和实体框架的代码优先迁移更新现有数据库 [ ^ ]
You can look here also: Update an Existing Database using Code First Migrations with ASP.NET and Entity Framework[^]


这篇关于如何在exisitng数据库上修复EF代码的第一种方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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