简体   繁体   中英

How can I specific columns using FromSqlRaw in EF.core

I just used FromSqlRaw. In Microsoft tutorial enter link description here , using FromSqlRaw has to select all columns (pls, I haven't seen some good examples as well). But what I want is to select some specific columns when joining several tables.

Firstly, I joined two tables as following shown (RequestMaterial has Request's Key as Foreign Key):

var requestVm = CurrentDbContext.PmrRequest
                .FromSqlRaw("Select [r].[RequestName] from [Request] as [r] " +
                            "LEFT JOIN [RequestMaterial] as [m] On [r].RequestId = [m].RequestId " +
                            "where [r].[InitiatorUserId] = 'xxxx'")
                            .ToList();

The error message is "The underlying reader doesn't have as many fields as expected".

When I tried to select a column without joining tables like:

var requestVm = CurrentDbContext.PmrRequest
                .FromSqlRaw("Select [r].[RequestName] from [Request] as [r] " +
                            "where [r].[InitiatorUserId] = 'xxxx'")
                            .ToList();

The same error is reported. Up to now, this problem can only be fixed when I select all columns. But the question is when I did this with joining tables, duplicated columns (RequestId) are selected with error reported ("An item with the same key has already been added. Key: RequestId'").

Does Anyone have similar experiences? Or Any solutions for the mentioned condition?

Create a special class to get data from sp. This class should have all properties that select of store procedure has. You don't need to select everytning. Just select what you need.

public class ResultData
{
public string RequestName {get;set;}
public string RequestMaterial {get;set;}
.....
.....
}

after this add to dbContext DbSet and config no key like this

modelBuilder.Entity<ResultData>(e =>
        {
            e.HasNoKey();
        });

And this a sample function to get data using the store procedure


public async Task<IEnumerable<ResultData>> GetDetailsData(int id, string name)
{
    var pId = new SqlParameter("@InitiatorUserId", id);
 
    return await _context.Set<ResultData>()
             .FromSqlRaw("Execute sp_GetData  @Id ", parameters: new[] { pId })
            .ToArrayAsync();
}

if you use ef core less then 3.0, use.FromSql instead of.FromSqlRaw

The DbSet must be included in the model (ie it can not be configured as Ignored). All columns in the target table that map to properties on the entity must be included in the SQL statement. The column names must match those that the properties are mapped to. Property names are not taken into account when the results are hydrated into instances of the entity.

If any columns are missing, or are returned with names not mapped to properties, an InvalidOperationException will be raised with the message:

'The required column '[name of first missing column]' was not present in the results of a 'FromSqlRaw' operation.'

Sorry, when I read the official tutiral, I found this

There are a few limitations to be aware of when using raw SQL queries:

The SQL query must return data for all properties of the entity type.

Therefore, currently, we are not allowed to specify columns using FromSqlRaw in EF.core 2.0+.

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