I have an Entity Framework .edmx file in which one of the models is based on a view in a SQL Server instance on Azure. I alter this view so that it includes one additional column.
In SQL Server Management Studio the ALTER
statement succeeds without incident. Afterward, SELECT * FROM theView
does show the new column as expected.
Turning to the solution in Visual Studio I open the .edmx file to show the design surface for models. I then right-click and select Update Model from Database. I'm shown the update wizard and switch to the Refresh tab. Many views are shown on the tab. This does include the one recently altered.
I click Finish and after several seconds the wizard completes. However, the view in the database no longer has the additional field as confirmed by running the SELECT
statement above and viewing its structure in Design mode. Unexpected, but consistent with the column now being missing, my solution encounters this error when trying to return the corresponding DbSet
:
System.Data.SqlClient.SqlException: Invalid column name
I've read a few related items that suggest there are problems as well as quirks to the Update Models from Database
feature:
- Update Model From Database (Database First)
- “Update Model from Database” does not see a type change
- Update model from database
Still, none of these quite mention the behavior I am seeing. Also I think my steps agree with this tutorial. It's admitedly dated, but everything except for the ultimate result seems to lineup in VS 2017.
Is my expectation that the generated models should conform to the database and not vice versa altogether incorrect? Am I Perhaps missing some step or setting.
UPDATE [2019-06-19]: Although the commentator who asked about it reconsidered this as a possible red herring, I'll mention it for the sake of completeness. The OnModelCreating
for the context looks like this (It's the default):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}