简体   繁体   中英

Scaffold-DbContext SQL Views ? ASP NET CORE 3.1

I need your help. I read on the web that there has never been a proper way to import views from sql into our asp.net core project. Do you know if in version 3.1 you can do ? If so, how? For tables I use the "scaffold-DbContext" command. Thank you very much!

Although you cannot use scaffold-DbContext for database view but you can still use SQL View in your .Net Core project.

Suppose you have the following SQL View

CREATE VIEW [dbo].[v_MyTableResult] AS

SELECT 
    SELECT Id, Name FROM MyTable

GO

Create a new Model class based on the result set of SQL View.

public class MyTableModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

In the database context class, introduce property for the Model.

public virtual DbSet<MyTableModel> MyTableResults { get; set; }

In OnModelCreating method, configure model properties

    modelBuilder.Entity<MyTableModel>(entity =>
    {
        entity.HasNoKey();

        entity.Property(e => e.Id)
            .HasColumnName("Id");

        entity.Property(e => e.Name)
            .HasColumnName("Name");
    });

Finally, in the method where you need results from the view, call FromSqlRaw on DbSet property

    var myTableResults =
        this.MyDbContext.MyTableResults.FromSqlRaw(
            "SELECT * FROM dbo.v_MyTableResult").ToList();

It seems not supported yet. For a workaround, you could refer to

Is it possible to automatically map a DB view on Entity Framework Core version 2.1?

It is possible to scaffold a view. Just use -Tables the way you would to scaffold a table, only use the name of your view. Eg, If the name of your view is 'vw_inventory', then run this command in the Package Manager Console (substituting your own information for "My..."):

PM> Scaffold-DbContext "Server=MyServer;Database=MyDatabase;user id=MyUserId;password=MyPassword" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Temp -Tables vw_inventory

This command will create a model file and context file in the Temp directory of your project. You can move the model file into your models directory (remember to change the namespace name). You can copy what you need from the context file and paste it into the appropriate existing context file in your project.

Note: If you want to use your view in an integration test using a local db, you'll need to create the view as part of your db setup. If you're going to use the view in more than one test, make sure to add a check for existence of the view. In this case, since the SQL 'Create View' statement is required to be the only statement in the batch, you'll need to run the create view as dynamic Sql within the existence check statement. Alternatively you could run separate 'if exists drop view…', then 'create view' statements, but if multiple tests are running concurrently, you don't want the view to be dropped if another test is using it. Example:

  void setupDb() {
    ...
    SomeDb.Command(db => db.Database.ExecuteSqlRaw(CreateInventoryView()));
    ...
  }
  public string CreateInventoryView() => @"
  IF OBJECT_ID('[dbo].[vw_inventory]') IS NULL
    BEGIN EXEC('CREATE VIEW [dbo].[vw_inventory] AS
       SELECT ...')
    END";

This is a helpful link. It describes adding the code sections by hand (as Nouman mentioned) instead of scaffolding them: https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=fluent-api

Yes, we can use scaffold-DbContext with view .

using scaffold-DbContext for database view (myview). I executed below scaffold-DbContext query.

Scaffold-DbContext "Data Source=my_server,1433;Initial Catalog=my_database;User Id=my_user;Password=my_password;" Microsoft.EntityFrameworkCore.SqlServer -o Entities -Context ContextName -t myview -f

Once you execute then you will get entity class and contextfile.

Code in entity class (myview.cs)

////// Fetching data
public partial class myview
{ 
    public Guid property_1 { get; set; }
    
}

Code in dbcontect file (ContextName.cs)

public partial class ContextName : DbContext
{
    public virtual DbSet<myview> myview { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<myview>(entity =>
        {
            entity.HasNoKey();

            entity.ToView("myview", "ExtIntDWFS");

            ////// other property configuration.
        }
    }
}

Code in controller to fetch view data

////// Fetching data
public async Task<myview> GetView(Base @base)
    {
        myview obj = new myview();

        using (ContextName context = new ContextName())
        {
            obj = await context.myview.Select(s => new myview
                                              {
                                                  property_1 = s.property_1,
                                              }).FirstOrDefaultAsync();
        }

        return obj;
    }

Above code is working for me successfully.

I recently had a similar need (retrieve one ore more views and create the corresponding classes in a .NET project). It can be done with Scaffold-DbContect , for example:

Scaffold-DbContext "Server=<address>;
Database=<dbname>;
Trusted_Connection=True" 
Microsoft.EntityFrameworkCore.SqlServer -OutputDir
ModelDirectory -t <table_name> -t <view_name>

It creates a folder called OutputDir with the corresponding classes for the indicated tables or views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM