Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use views in code first entity framework [closed]

How can I use the database view in entity framework code first,

like image 374
Sagar Avatar asked Sep 18 '11 11:09

Sagar


People also ask

Can we use views in Entity Framework?

Views can be used in a similar way as you can use tables. To use view as an entity, first you will need to add database views to EDM. After adding views to your model then you can work with it the same way as normal entities except for Create, Update, and Delete operations.

How do I add a view to code first?

Accepted Answer. You cannot create views with EF Code First approach. If you want to create view then execute creation sql script in Seed method. But you'll still not be able to map entity to this view, except hacking model by creating and droping table with same name as your view will have.

How do I create a view using EF Code First poco?

You cannot create a view from EF Code First ,you should add the script in the 'seed' script to pre populate the view .


2 Answers

If, like me, you are interested only in mapping entity coming from an other database (an erp in my case) to relate them to entities specific of your application, then you can use the views as you use a table (map the view in the same way!). Obviously, if you try to update that entities, you will get an exception if the view is not updatable. The procedure is the same as in the case of normal (based on a table) entities:

  1. Create a POCO class for the view; for example FooView

  2. Add the DbSet property in the DbContext class

  3. Use a FooViewConfiguration file to set a different name for the view (using ToTable("Foo"); in the constructor) or to set particular properties

    public class FooViewConfiguration : EntityTypeConfiguration<FooView>       {     public FooViewConfiguration()     {         this.HasKey(t => t.Id);         this.ToTable("myView");     } } 
  4. Add the FooViewConfiguration file to the modelBuilder, for example overriding the OnModelCreating method of the Context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {     modelBuilder.Configurations.Add(new FooViewConfiguration ()); } 
like image 97
Daniele Armanasco Avatar answered Oct 14 '22 08:10

Daniele Armanasco


This may be an update but to use views with EF Code first simply add [Table("NameOfView")] to the top of the class and all should work right without having to go through all the hoops everyone else is going through. Also you will have to report one of the columns as a [key] column. Here is my sample code below to implement it.

using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;  namespace SomeProject.Data {     [Table("SomeView")]     public class SomeView     {         [Key]         public int NameID { get; set; }         public string Name { get; set; }     } } 

And here is what the context looks like

using System.Data.Entity;  namespace SomeProject.Data {     public class DatabaseContext : DbContext     {         public DbSet<SomeView> SomeViews { get; set; }     } } 
like image 45
Al Katawazi Avatar answered Oct 14 '22 07:10

Al Katawazi