1

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.

3
  • Are you using code first approach? Commented Mar 16, 2018 at 3:21
  • yes I do. I think with ABP framework Code first is the only available option, isn't it? Commented Mar 16, 2018 at 3:31
  • Mention you EF and Project template version Commented Mar 16, 2018 at 3:36

3 Answers 3

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @vivek. would you please also explain what the step 4 does.
If you don’t do this, it will try to create table ‘YourViewName’. So by doing this step you are enforcing not to create table, instead you will create a view by using SQL.
1

Create your table view in the database. Then using EF FluentAPI to config the view mapping. Here is the sample code:

1. Create POCO class to map:

 public class YourView
 {
        public int Id { get; set; }

        public string Value { get; set; }
 }

2. EF FluentAPI mapping configuration:

Create map class:

 public class YourViewMap : IEntityTypeConfiguration<YourView>
 {
        public void Configure(EntityTypeBuilder<YourView> builder)
        {
            builder.ToTable("YourViewName");
        }
 }

Add mapping configuration to your DbContext (such as AbpCoreDbContext). Override OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfiguration(new YourViewMap ());
    base.OnModelCreating(modelBuilder);
}

3. Get data:

using IRepository<YourView> to query data from the view.

P.S: related question how to use views in code first entity framework

Comments

0

Create a new DbContext for your legacy database. You can have multiple DbContext in an ABP application. Each DbContext have its own connection string. Creating view is kinda hackish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.