I have to created a database through Entity Framework 5 with the following model:
public class Post
{
public int PostId { get; set; }
[MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Then I have added new property in Post
public string Abstract { get; set; }
then I have run
Add-Migration AddPostAbstract
which created the following class in my Migrations
folder, after that I have modified this file by adding one more SQL statement
//201308300714477_AddPostAbstract.cs
public override void Up()
{
AddColumn("dbo.Posts", "Abstract", c => c.String());
Sql("UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL");
}
public override void Down()
{
DropColumn("dbo.Posts", "Abstract");
}
After that I have executed this command
Update-Database –Verbose
which updated my database and also returned this SQL query:
ALTER TABLE [dbo].[Posts] ADD [Abstract] [nvarchar](max)
UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL
Now I have deleted Abstract
column from table and tried to execute the above query manually in SQL at that time it show me following error.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Abstract'.
My question is why this query is not executed in SQL even this query is generated through migration?
Or is there any way to run such multiple query through migration generated script.