Search for existing suggestions

Database VIEW creation support and specific mapping of entites to views.

Currently, to get Code First to create a database with a VIEW, the CREATE VIEW sql statement must be hacked a couple of Context.Database.ExecuteSqlCommand's. One to drop the table that EF created for the entities you're mapping to it and another to create the view using the same name as the table EF assumes is still there.

When mapping an entity to a view, you have to use modelBuilder.Entity<T>.ToTable and treat the view as a table. Entity Framework assumes the view is still a table and assumes it's read/write. A new method of .ToView might be added to tell EF to "trust me, the view will be there" and to give options to tell it if the view is readonly or read/writeable.

13 votes
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

Anonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • tschmit007 commented  ·   ·  Flag as inappropriate

    allow to define a view by code first.

    actually we can map an entity to a view but it then becomes nasty to:
    - generate the database,
    - migrate the database
    - evolve the schema in general

    It should be possible to:
    - declare a DbSet that does not involve a table creation
    - declare a view, with a linq query for example, that allows view creation.

    For DbSet if should be a convention: if table name starts with vvv then it is a view, do not create a table

    For view creation... no idea actually.

Feedback and Knowledge Base