数据库中已经有一个名为“AspNetUsers”的对象 [英] There is already an object named 'AspNetUsers' in the database

查看:160
本文介绍了数据库中已经有一个名为“AspNetUsers”的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了互联网,堆栈溢出,但我感到迷失在实体框架迁移的奥秘。

I have searched all over the internet, stack overflow but i am feeling lost in the mystery of Entity framework Migration.

我有现有的数据库。我添加了三个与AspNetUsers表有一些关系的表。

I have existing database. I have added three new tables which have some relation with AspNetUsers table.

我已经尝试过两种方法,使用pmc将这些表添加到数据库。
首先注释掉dbcontext中的所有新表,然后使用以下命令。

I have tried two ways to add these tables to database using pmc. first comment out all new tables in dbcontext and then use following command.

Add-migration initial -ignorechanges

然后从dbcontext中取消注释表并运行update-database命令。结果没有添加新表。

then uncomment the tables from dbcontext and run update-database command. result no new table added.

然后我删除迁移历史记录表中的所有条目,并删除迁移文件夹,并从头开始。

then i remove all the entries in migration history table and remove migration folder and start from scratch.

  enable-migrations -EnableAutomaticMigrations
  Add-migration bcvn
  update-database -verbose

现在,当我运行更新数据库时,它说在数据库中已经有一个名为AspNetUsers的对象。发现这篇文章是有帮助的,但在我的情况下AspNetUsers表中没有更改,因此我可以按照这篇文章 http://blog.rajsoftware.com/2014/07/12/entity-framework-code-first-automatic-migration-existing-tables/ 请有人救我。可能我会收到很多downvote,但我不在乎我想要解决方案。

now, when i run update database, it says There is already an object named 'AspNetUsers' in the database.Found this article to be helpfull but in my case there are no changes going on AspNetUsers table in up method so i can follow this article http://blog.rajsoftware.com/2014/07/12/entity-framework-code-first-automatic-migration-existing-tables/ Please someone save me.May be i will receive many downvote .but i dont care i want solution.

推荐答案

得到它的工作。 Trick是将所有与新表相关的代码,特别是我之前没有做过的关系代码注释到ApplicationUser类然后

Got it working. The Trick was to comment out all the code related to new table especially the relationship related code which i was not doing before to ApplicationUser class then

Add-migration initial -ignorechanges

然后取消注释所有代码,然后

then uncomment all the code and then

update-database -verbose

我以前没有做的唯一的事情是评论这样的关系 -

The only thing which i was not doing before was to comment out the relationship like this--

//  public virtual Notification Notifications { get; set; }

和通知表

//  public virtual ApplicationUser ApplicationUser { get; set; } 

可能对某人有帮助。

这篇关于数据库中已经有一个名为“AspNetUsers”的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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