简体   繁体   中英

Entity Framework Core - binding SP return value to project Model without re-mapping

In below scenario, The execution of stored procedure is returning the known columns matching with type "LocalModel"

Is there a way in EF Core to query entity and bind that back to anonymous type.

List<LocalModel> ModelList= context.Table.FromSql(Stored_Proc +" @Param", _moduleName).ToList(); 

Like, In normal EF this can achieved as below.

List<LocalModel> ModelList= context.Database.SqlQuery<LocalModel>("exec Stored_Proc").ToList<LocalModel>();

If you use FromSql on the DbSet of the type you want to bind to, then it will create instances of that POCO class. For example:

Assuming:

public DbSet<LocalModel> LocalModels { get; set; }

Then:

context.LocalModels.FromSql(...).ToList();

If you're working generically, or just if you prefer, you can use Set<T>() instead of the actual DbSet property name:

context.Set<LocalModel>().FromSql(...).ToList();

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