-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 to Create One to Many Mapping in relation to stored procedure #16863
Comments
@jt4000 Can you explain more what you mean by, "using stored procedure?" |
Hi ajcvickers, I have updated the content. |
@jt4000 Do you want to be able to call the |
Call the SP GetData and retrieve the data. And then the data should be added in the class Blog and class Post. The class Blog contain a data member that is List. Based on the picture (sql code result) this specific blog has two data that is Post. In the end it will be a One to Many Mapping. A blog has many Post. |
@jt4000 I'm still having trouble understanding what you want to do. Could you post the code you have to do this so far, and indicate where it's not doing what you want? |
Let's say I have this simple Blog database structure with 4 tables: and some sample data in each table looks like this: And I have this SQL script.
There are a few ways to execute this script with EF Core. One is to call this sql script directly from the code. Another way to create a Store Procedure or View and call that from the code.
This is a function in a Controller:
How I can map the result of sql script to the Blog object so that I can have the following result?
|
@tola So you want to take a custom flat result set like this:
and have EF materialize a graph of entities? If so, EF can't do that. It can only materialize into types that match your result set. |
@ajcvickers I see. It's clear now. I have been trying many ways but no good result. I know that in Dapper that's a splitOn option which is to tell Dapper how to split the flat dataset and map it to a model. Also, there's an Sapper.Dapper solution for Dapper to handle this kind of mapping data set with objects. Often time, I work with SQL script or Stored Procedure that were given to use in the code from DB admin and I do not have much control of them. If possible, I'd like to use EF Core only without having to add another micro ORM to the project. Is there any other way that I can implement custom mapping solution to this. If possible, I'd like to see some features of Sapper.Dapper in EF Core in the future. I'm sure some of other developers are dealing with Stored Procedure, View and SQL Script quite often as well. It's just time consuming to go back to ADO.NET and map the sql result manually. |
Related #14525 |
Closing as a duplicate of #14525 with a note added there to consider this specific scenario. |
Goal:
Create a one to many mapping by using stored procedure GetData
In other words, call the SP GetData and retrieve the data. And then the data should be added in the class Blog and class Post.
The class Blog contain a data member that is List. Based on the picture (sql code result) this specific blog has two data that is Post. In the end it will be a One to Many Mapping. A blog has many Post.
Problem:
how do you create a one to many mapping in relation to a stored procedure? One Blog has many [Post]
Info:
the source code is from this page: https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db?toc=%2Faspnet%2Fcore%2Ftoc.json&bc=%2Faspnet%2Fcore%2Fbreadcrumb%2Ftoc.json&view=aspnetcore-2.2
I'm using Entity Framework Core 2 and ASP.NET Core.
Using C# code from
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db?toc=%2Faspnet%2Fcore%2Ftoc.json&bc=%2Faspnet%2Fcore%2Fbreadcrumb%2Ftoc.json&view=aspnetcore-2.2
The text was updated successfully, but these errors were encountered: