Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would use a stored procedure with query types? #1018

Closed
THammond9 opened this issue Oct 3, 2018 — with docs.microsoft.com · 12 comments
Closed

How would use a stored procedure with query types? #1018

THammond9 opened this issue Oct 3, 2018 — with docs.microsoft.com · 12 comments

Comments

Copy link

This document only shows an example of a view. How would you use a stored procedure with query types to return something that is not "*" and more than one column?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Copy link

You can do this, given you have a DbQuery<BlogPostsCount> BlogPostCounts in you model:

modelBuilder.Query<BlogPostsCount>().ToQuery(() => BlogPostCounts.FromSql(<SQL to execute stored procedure>));

Copy link

What about passing parameters to the sp on runtime?

@ngoctubk
Copy link

ngoctubk commented Nov 6, 2018

You can pass parameters to SP on runtime like this:

  1. In your Context class, add OnModelCreating

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Query();
}

  1. Call SP:

var testList = Context.Query().AsNoTracking().FromSql(
"SP_Name @p0, @p1",
"p0 value",
"p1 value"
).ToList();

@mtsiakiris
Copy link

Thank you for the information.
BTW, I am using views with EF Core, I should have figured this out earlier.
But, one more question arises, how can you handle multiple result sets from an SP?
Haven't tried that so far... I suppose EF preserves the last one?

@ngoctubk
Copy link

ngoctubk commented Nov 7, 2018

I don't know how to handle multiple result sets but in this case EF will get the first result set to list.

@divega divega added this to the 2.2.0 milestone Nov 10, 2018
@divega divega self-assigned this Nov 10, 2018
Copy link

We are just looking into EF Core 2.x. I am trying to understand the use of Stored Procedure calls and how to use them with EF Core. While this example shows how to return a query result from a stored procedure. How would you set one up that returns the return results of a stored procedure. We have a few stored procedures that do bulk updates or other things that don't have a query result, but does return a return value (in some cases a bool, string, int, etc...). Examples would be greatly appreciated.

@divega divega modified the milestones: 2.2.0, 3.0.0 Feb 21, 2019
@progmars
Copy link

progmars commented Aug 5, 2019

I'm also having the same issue. My stored proc returns a list of records without any unique key and I need to map it to my entity because field names in the database are chaotic (legacy database).

So, as usual, I add:

    public class MySprocResultEntityConfiguration : IEntityTypeConfiguration<MySprocResultEntity>
    {
        public void Configure(EntityTypeBuilder<MySprocResultEntity> builder)
       {
            ... mapping all ugly db field names here; not using ToTable or ToView because the entity comes from sproc

But EF throws on initialization:

System.InvalidOperationException: The entity type 'MySprocResultEntity' requires a primary key to be defined.
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model)

So, how do I add custom mapping to my query type that has no primary key?

@progmars
Copy link

progmars commented Aug 5, 2019

Oh, I just found IQueryTypeConfiguration, which was not documented in the article - I just replaced IEntityTypeConfiguration with IQueryTypeConfiguration, mapped my entity to my stored proc results without HasKey, and used the following code:

var sprocResults = await _dbContext.Query<MySprocResultEntity>()
                .FromSql("Get_MySprocResultEntities @Param1 = {0}, @Param2 = {1}", p1, p2)
                .AsNoTracking()  // I guess, this is redundant but I like to be explicit
                .ToListAsync();

I think IQueryTypeConfiguration should be added to the Query Types article.

@gopala000
Copy link

@progmars How did you define the query expression to builder.ToQuery() for stored procedure? I'm going through the same to call a sproc. A sample will be helpful.

@progmars
Copy link

@gopala000
In my case, I didn't use ToQuery at all. I just defined all my entity fields using builder.Property and that was enough.

@gopala000
Copy link

gopala000 commented Aug 14, 2019

@progmars Thanks for your response. I'm looking for how to map the sproc and parameters using builder.*. In addition looking for how to pass the parameter values as well.

@ajcvickers
Copy link
Member

Duplicate of #969

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants