Search code examples
entity-frameworkviewaspnetboilerplatesql-view

How to create View (SQL) from Entity Framework in ABP Framework


My situation is that I need to query from another database and show the result in my application. Both databases are on the same server. I came up with an idea on creating SQL-View in my database which would query the other database for values that I want. But I am not quite sure on how I can create or map SQL-View from the ABP framework?

I am using the full .Net framework with the Angular template.


Solution

  • Creating View is not directly supported in EF. So you can try the below approach.

    • Create an empty Migration using Add-Migration.
    • Write you Create View script in Up method of generated migration and run the script using context.Database.ExecuteSqlCommand method.
    • Declare your class and use Table as you do for your model class.

    [Table("YourViewName")] public class YourClassName {

    }

    • Ignore your view class like this modelBuilder.Ignore<yourClassName>(); in OnModelCreating method.
    • Run Update-Database in Package Manager Console.