0

I am working on a C# project that includes a MySQL database and EntityFramework layer. I am working with Code First. (This is because i work with xamarin/mono. There is no visual designer).

I want to create some mysql views (for performance reasons). How should i do to create the view ? I've created all views in mysql but is there a way to "describe" this view in a C# class, in order to make it work with Entity Framework ?

testpresta
  • 429
  • 5
  • 15

1 Answers1

0

Working with views is the same as working with tables. You just map them using code first attributes or fluent configuration. The only thing that you can't do is update entries, you can just query the data.

public class MyViewConfiguration : EntityTypeConfiguration<MyView>      
{
    public MyViewConfiguration()
    {
        this.HasKey(t => t.Id);
        this.ToTable("myView");
    }   
}
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • I am working with simple objects and annotations. Is there an annotation to say the class is read only ? And i do not want to generate a table when i initialise the database the first time – testpresta Dec 07 '15 at 11:39
  • To turn off database creation: Database.SetInitializer(null). I don't know of any ReadOnly keyword... – L-Four Dec 07 '15 at 12:23
  • Entity framework always needs a primary key. See http://stackoverflow.com/questions/3996782/entity-framework-table-without-primary-key – L-Four Dec 07 '15 at 12:24
  • Where should i put this SetInitializer. What i want to do is to generate empty database structure, without views. And how should i do to simulate a fake primary key ? – testpresta Dec 07 '15 at 13:16
  • SetInitialize can go in your DbContext constructor. Primary key: did you read the answers in the link I just posted? – L-Four Dec 07 '15 at 13:22
  • I do not want to turn off database creation, i want to turn off creation of specific tables only (the views) – testpresta Dec 07 '15 at 15:53