I wanted to select rows from a view using Linq to Entities, so I right clicked the edmx designer and “generate model from database”, then selected the view from the wizard. The view appears. I can select rows.
However, from this point on, “generate database from model” now results in this (benign i think but annoying) error:
Msg 2714, Level 16, State 6, Line 3
There is already an object named 'vAdvertiserEmployees' in the database.
Msg 4909, Level 16, State 1, Line 3
Cannot alter 'dbo.vAdvertiserEmployees' because it is not a table.
What am I doing wrong here?
Update model from database and Generated database from model are two exclusive approaches. Built-in VS EF designer is not able to go from one to another without very big pain – once you start using database constructs like views the way back to generating model from the database is gone. The view in EDMX file is represented as SQL query selecting from the view (inside element called
DefiningQuery). If you run Generate database from model whole database description (SSDL part) is deleted from EDMX file and replaced with a new one which doesn’t allow custom SQL queries (model doesn’t know anything about DB constructs like views). It means that all your mapped views will became tables for EF and next step will create scripts for creating / changing tables (changing only with additional tools like Database Generation power pack).Choose one approach and follow it for whole development or use (buy) some extension to designer which will allow you swapping between these approaches. As an example of tools which allow you selectively update both model and database check Huagati EDMX tools.