1

I am using entity framework and Codefirst to create a Stored procedure. As I reed, we should do some steps in package manager to create SP.

1 - enable migration

2- add-migration MYclass

3- update-database

and in Myclass(new created) I write my SP code in UP() method to create SP. works fine!! BUT when I change SP and run update-database again it does not work and I need to do another add-migration command to create new MYclass2. Is that right? I mean every time I should write add-migration?

this is mycode

  public override void Up()
        {
            Sql(@"Create procedure testSp 
                        as 
select std.Id as stdName, std.Name as MajorName, mj.Name from dbo.Students as std
inner join dbo.Majors as mj on std.Id = mj.Id
");
        }

        public override void Down()
        {
            Sql("drop procedure testSp");
        }

when I run update-database again the result is "No pending explicit migration"

even if I change the SQL query to "Alter procedure...." it does not work , no change happens. thanks

EDITED

consider this scenario, I want to change my SP name(just an example) so I need to change the query to "Alter store procedure tespSP2 ..." or any other change, should I run add-migration again?? or update-database is supposed to do it??

  • Various techniques discussed [here](https://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity-framework-4-1-code-firs). – Steve Greene Sep 14 '17 at 15:12

1 Answers1

0

in entity framework migrations added to a _migrationHistory table, you can delete the last row of this table [it's not recommended]

or you can use rollback/undo command

Update-Database -TargetMigration:MigrationsName

then use update-database -force

1SaeedSalehi
  • 364
  • 1
  • 8
  • 16
  • So what will happen after this code? consider I want to change my storePr query, what should I do step by step? –  Sep 14 '17 at 07:41
  • you can add another migration and change your code there, it's the best way or if you are not in production you can do it manually, first remove the last row of the _migrationHistory table and then run the update-database command , it will be re update your database with the last migration – 1SaeedSalehi Sep 14 '17 at 07:55
  • but when we use Code first it is supposed to not access to SQl server and its tables. I thought update database should do it automatically!!! So ther is not any REwirte on migration, am i right?? every time we should create new migration. –  Sep 14 '17 at 07:59
  • 1
    yes you right, I suggest it get around entity-framework rules you need to add a new migration every time you change a query – 1SaeedSalehi Sep 14 '17 at 08:02