0

I have tried as below :

  1. removed all the generated scripts which it relates to model on the Migrations folder.

  2. removed all the entries whcih it relates to model on the __MigrationHistory table.

But it creates like this when I run:

PM> Add-Migration "Added_Investors_Table"

Generated migration script

 public partial class Added_Investors_Table : DbMigration
    {
        public override void Up()
        {

        }

        public override void Down()
        {

        }
    }

Model

    [Table("Investors")]
    public class Investor : FullAuditedEntity
    {

        public virtual int AmountCommitted { get; set; }

        public virtual decimal PercentageOfDeal { get; set; }

        public virtual decimal AmountSpentToDate { get; set; }
}

DbContext.cs

public class IpDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
        /* Define an IDbSet for each entity of the application */

        public virtual IDbSet<Investor> Investors { get; set; }

        public IpDbContext() : base("Default")
        {

        }
   }

Do you know why it does not recreate a migration script as usual ? What are the other places, should I remove this model other than the above mentioned 2 places ?

Note : The above mentioned table is a new one.I have successfully created script first time.But due to some changes and etc.. I need to recreate it again.But it does not work.But If I change existing model then it gives change set script.Very strange no ? I don't know where's the problem is..

5
  • can you show more code? like your DbContext class? Commented May 30, 2016 at 18:30
  • @tmg Done that.Please see it. Commented May 30, 2016 at 18:33
  • I've run into something similar in the past and got around it by creating an initial migration with -IgnoreChanges. Have a look at my answer here for the steps. Might work for you. Commented May 30, 2016 at 19:39
  • @Tone I cannot do that.B'cos I have around 50+ tables with all full of data.The above mentioned table is a new one.I have successfully created script first time.But due to some changes and etc.. I need to recreate it again.But it does not work.But If I change existing model then it gives change set.Very strange no ? Commented May 30, 2016 at 19:52
  • The problem is that EF stores a snapshot of your Current model in the last migration you scaffold. The VERY last migration is not necessarily the last migration that did something with Investor. Thus, your VERY last EF migration contains information about the Investor entity, even after deleting any previous code migration or __MigrationHistory entry related to Investor. You have to find a way to trick EF to believe it has a model without Investor in it, and then create a new migration to scaffold Investor again Commented May 31, 2016 at 3:39

1 Answer 1

2

I'm assuming you don't mind recreating the Investor entity and the corresponding table, but you don't want to modify anything else. If so, I think you can, carefully, try the following:

  1. Backup your database (use a prod copy or dev database anyway)
  2. Restore your previous Migration scripts back into your Migrations folder
  3. Restore the __MigrationHistoryTable
  4. Keep the migration script classes but remove the DDL code that is related to the Investor entity (e.g. If you have a migration to add Investor, the up and down methods should be empty for that migration)
  5. Remove the Investor entity and its configuration from the DbContext and generate a new migration using the -IgnoreChanges switch. Let's say this migration is called RemoveInvestorMigration. This one should also have empty up and down methods due to the -IgnoreChanges switch
  6. Apply the RemoveInvestorMigration migration to your (development) database via Update-Database. Now EF believes you removed the Investor entity (and you did, somehow)
  7. Restore the Investor entity and its configuration into the DbContext again
  8. Generate a new migration. Let's call this one RestoreInvestorMigration. This one should generate the proper up and down methods that create the Investor entity. You must have some CreateTable code here, otherwise something is wrong
  9. Apply the RestoreInvestorMigration and you are done

The trick is to make EF believe, but you have to be careful, because it will believe

By the way, whenever you need it, you can run Update-Database with the -Script switch, so that EF will show you the SQL code generated in order to update the DB after applying a migration. This will not apply the migration (i.e. it won't execute the SQL code). This way, you can see what EF would do should you choose to apply the migration

Sign up to request clarification or add additional context in comments.

1 Comment

I gave a temporarily solution for my client.I'll look at your solution on coming days and will give you my feedback.Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.