Search code examples
c#stored-proceduresentity-framework-coreasp.net-core-mvcdatabase-first

Entity Framework Core Database First not render Model of Stored Procedure?


Database

I have Stored Procedure name GetAllCustomer


I have Stored Procedure in the EntityFramework 6x and in Project ASP.NET MVC5.

I'm using by calling db.GetAllCustomer.ToList();

But in the EntityFramework Core it's not rendering DbSet and I need to create it manually. It can't use EF 6x.

Here is the image of my works:

enter image description here

Is there a way to call Stored Procedure as simple as EF 6x??


Solution

  • EF Core Power Tools can map stored procedures for you, it is not a built in feature of EF Core.

    Sample user code:

    using (var db = new NorthwindContext())
    {
            var procedures = new NorthwindContextProcedures(db);
    
            var orders = await procedures.CustOrderHist("ALFKI");
            foreach (var order in orders)
                Console.WriteLine($"{order.ProductName}: {order.Total}");
    
            var outOverallCount = new OutputParameter<int?>();
            var customers = await procedures.SP_GET_TOP_IDS(10, outOverallCount);
            Console.WriteLine($"Db contains {outOverallCount.Value} Customers.");
            foreach (var customer in customers)
                Console.WriteLine(customer.CustomerId);
    }
    

    Read more here: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#sql-server-stored-procedures