简体   繁体   中英

EF7 beta8 FromSQL

For the EF7 FromSQL parsing sql query to get the entity list.

var accounts = _context.Set<Account>().FromSql("SELECT TOP 10 * FROM Account WITH  
(NOLOCK)").ToList();

this above will return the entity list, but for certain condition.

var report = _context.Set<AccountReport>().FromSql("SELECT a.Id, a.Username, p.Item,
p.Quantity FROM Account AS a WITH (NOLOCK) INNER JOIN Purchase AS p WITH (NOLOCK) ON 
p.AccountId = a.Id WHERE p.CreatedOn BETWEEN '2015-11-01' AND '2015-12-01').ToList();

The model "AccountReport" is not an entity model, it will encounter with this error message.

:

Microsoft.Data.Entity.Metadata.ModelItemNotFoundException' occurred in EntityFramework.Core.dll but was not handled in user code

Additional information: The entity type 'AccountReport' was not found. Ensure that the entity type has been added to the model.

This should help when you try to fetch data which is not bound to DBSet.

EF7 Fetch dynamic data, not bound to DBSet

Other way is to write LINQ as done here

Linq to fetch dynamic data in EF7

在选择查询中,由于选择查询已分配给实体AccountReport,因此必须选择AccountReport的所有非空列。

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