Aflred
Aflred

Reputation: 4593

Entity framework Migration add new column to view

I have a view

[Table("View1")]
    public class View1Model
    {
        [Key]
        public int Id { get; set; }
        public int Age { get; set; }
     }

I would like to add another public int Weight to the table because in the sql database i updated the view, however the migration wont work.

It says when i try to update database that

Cannot alter 'dbo.View1' because it is not a table.

I know that i can remove the 'table' attribute but that will not work because i need to check in my code.Out of the question

Upvotes: 3

Views: 2296

Answers (1)

baochan
baochan

Reputation: 381

When you add-migration it will try to AddColumn() in the Up() and DropColumn() in the Down(). You'll want to remove those lines from the migration and manually change the view with a Sql(@"ALTER VIEW ...") command in which you modify the view directly in SQL. This will accomplish what the automatic migration was trying to do.

Upvotes: 7

Related Questions