Datareader is incompatible with the specified 'result set complex type'. A member of the type - entity-framework

I have a stored procedure that updates a date column in a table in MS SQL Server database. This stored procedure does not have any complex logic. It does a simple update on the date column with the value passed in its input parameter. This stored procedure does not return any result set upon successful execution but returns a result set with a few columns with names ErrorNumber, ErrorSeverity etc., when the stored procedure execution throws an exception from the try..catch exception handling block.
I am using DB First entity approach; I have updated Entity Data Model for this stored procedure from my asp.net MVC web application. Entity framework designer generated a complex type for the return type on this stored procedure with a property for each of the columns in the columns list returned in the catch block. When I invoked the stored procedure from asp.net MVC application with valid input parameters, I encountered the "Datareader is incompatible exception". I know this exception is triggered by entity framework because the stored procedure execution does not return a result set and the framework is expecting a column to be returned back with the name "ErrorNumber". I invoked the stored procedure directly with the db context instance with required valid parameter values.
for example db.UpdateProcedure(dueDate). I did not use a data table to load the data from the data reader. I know loading the result set into a data table will not cause this exception.
After making a minor change to the stored procedure to return ##rowcount on successful execution, the application did not throw this exception upon this stored procedure invocation.
I am wondering if there is a better way to deal with this situation without updating the stored procedure to return ##rowcount.
I am using the designer generated entity model from the database; I did not make any manual changes to the schema after auto-generation of the entity model. To be honest, I don't like doing manual changes to the schema once it is auto-generated.
Is there anything I am missing during the entity schema update for this stored procedure to have correct complex type generated when the model is updated from entity designer?
Is the stored procedure returning a result set upon exception occurrence and not returning any result upon successful execution causing any problem to the process that updates entity schema?
Your help is much appreciated.
Thank you in advance.

Related

Call Stored Procedure from Entity Framework that returns compound object

I have a database first setup in .net6
I need to call a stored proc (Lets say it is called GetMyData)
The stored proc is going to return 5 columns from various tables and so does not map to a current entity model.
The only things I can find is to use the context model to return a stored proc result but that will not work as the result set does not map to an existing model. Or a convoluted ADO.Net style call that looks horrific and needs lots of code.
Is there a simple way to call a stored proc that returns various columns from many tables?

EF Core stored procedure with 2 parameters error System.InvalidOperationException:

I am moving to EF Core and trying to call a stored procedure with 2 parameters.
The current setup for 1 parameter works fine.
Here is the error I see:
System.InvalidOperationException: FromSqlRaw or FromSqlInterpolated was called with non-compostable SQL and with a query composing over it. Consider calling AsEnumerable after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client-side. at
Here is my repository call:
public IQueryable<PartsGridDTO> GetPartsGridQueryable(int partId, int groupId)
{
return MoreContext.SpPartsGridDetailYourCases
.FromSqlRaw($"EXECUTE dbo.GetProductsByPartAndGroup {partId}, {groupId}")
.Select(s => new PartsGridDTO
{
PartId = partId,
//....
}).AsQueryable();
}
I've tried some fixes from here:
Include with FromSqlRaw and stored procedure in EF Core 3.1
but none of those work.
I am returning AsQueryable() up until the controller and then calling .ToListAsync().
I tried adding .IgnoreQueryFilters(); right before .AsQueryable(), but I still see the error.
Any thoughts? Am I passing in the parameters wrong? Is it an EF Core issue or?
There were 3 items until I resolved the issue:
I had extra properties that were not within the return of the stored procedure. I guess that causes the error I posted above.
(A sub-issue) I also only needed to return items that the grid was going to display. Meaning the grid might show only 5 of the 8 values that are returned within the stored procedure. Having those extra columns in the return items of stored procedure called caused this error later: InvalidOperationException: The required column 'Address' was not present in the results of a 'FromSql' operation
Here is the final look of my stored proc code:
return Context.EntityExample.FromSqlRaw($"dbo.GetProductsByPartAndGroup {partId}, {groupId}")

C# Entity Framework - How to generate object type for input parameters of stored procedure

I have a stored procedure that gets many input parameters (the procedure persoms Insert statement)
I use EF to access this procedure.
Is there a way to automatically generate object type that contains all the input parameters of the procedure?
Something like the complex type that is being generated for the output of the procedure.
My EF version is 6.1.3
No, I don't think it's possible to automatically generate a class for the input parameters of a stored procedure in EF 6.1.3.
This article helped me in working with insert stored procedure with the entity framework:
http://www.entityframeworktutorial.net/EntityFramework5/CRUD-using-stored-procedures.aspx

What type of object is returned by EF stored procedure call?

I am using Entity Framework (v6.1) with the Database First method. I have created my EDMX file and it references a stored procedure I selected while creating the EDMX via the EF wizard. My question is how do I use what the stored procedure returns?
Below is my dbContext and the name of the stored procedure GetHtmlContent. The stored procedure takes contentId as an integer parameter.
Here is the SQL of the stored procedure GetHtmlContent ..
SELECT
HtmlContent.contentId,
HtmlContent.[Name] AS 'Name',
HtmlContent.HTML AS 'HTML',
HtmlContentCategory.CategoryTitle
FROM HtmlContent
LEFT JOIN HtmlContentCategory ON HtmlContent.CategoryID = HtmlContentCategory.HtmlContentCategoryID
WHERE HtmlContent.ContentId= #ContentId
Below is what I get when I hover over the stored procedure method contained within the dataContext to see what is returned.
System.Data.Entity.Core.Objects.ObjectResults<GetHtmlContent_Result> dbContext.GetHtmlContent(contentId)
This is where I am getting confused ...
Is the <GetHtmlContent_Result> a type representing the dataset returned by the stored procedure?
What is System.Data.Entity.Core.Objects.ObjectResults?
Since I am accepting a collection of records from the stored procedure do I need to create a class to hold the results of the stored procedure?
Is the a type representing the dataset returned by the stored procedure?
GetHtmlContent_Result is representing each record that is produced by stored procedure.
HtmlContent.contentId,
HtmlContent.[Name] AS 'Name',
HtmlContent.HTML AS 'HTML',
HtmlContentCategory.CategoryTitle
What is System.Data.Entity.Core.Objects.ObjectResults?
The ObjectResult is an enumerable collection class, but it is a
forward-only collection so once it has been enumerated, you cannot
enumerate it again. For example, if you call ToList on the result,
e.g., GetDetailsForOrder(3).ToList(), then you cannot subsequently
provoke another enumeration by calling ToList again, binding the
results to a control or executing a foreach over the results. - MSDN
Since I am accepting a collection of records from the stored procedure do I need to create a class to hold the results of the stored procedure?
It's just like other entities that represent tables in the database, a POCO. You can use it directly.

stored procedure mapping Entity Framework

We're using a Function Import in an EF4 model to populate an existing entity in our Model. The entity in the model has a Key field of Id which we're struggling to map as our stored procedure doesn't return an Id field. I've tried setting the value in the mapping to a literal value of 0 but that fails with an EntityCommandExecutionException and the following exception text.
The data reader is incompatible with the specified 'Candidate'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.
Short of modifying the stored procedure to return a dummy Id field can anyone recommend what the best approach is for this as the dummy field option feels very clunky to me.
Many Thanks
If you can't return enough data to fully materialize the entity -- and the Id field is certainly going to be required for that -- then you need to change the return type on the proc to be a complex type instead of an entity.
Use another POCO class with the same structure to receive the results of the stored procedure call, here's an example:
string sp = string.Format("EXEC dbo.spComercialesAsociadosActivos {0}", idComercialPrincipal);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<InfoComercial>(sp);
In this case "InfoComercial" is a POCO class with the same structure as "Comercial", which is tied up to EF code first in the DBContext, then I used this independent class in the viewModel to create a disconnected "Comercial", it's not an ideal solution but will work fine until EF 5 comes with SP support.