entity-framework-core - 如何将“FromSql()”查询的结果建模为基本数据类型?
问题描述
我正在使用 EF Core 3.1 中的无键实体类型功能。我想将一些表数据映射到我的实体类中。这是我的db-context
配置:
public class HimartWarehouseReportContext: DbContext
{
public HimartWarehouseReportContext()
{
}
public HimartWarehouseReportContext(DbContextOptions<HimartWarehouseReportContext> options)
: base(options)
{
}
public DbSet<MarketPresence> MarketPresence { get; set; }
public DbSet<Campaign> Campaign { get; set; }
public DbSet<CampaignDt> CampaignDt { get; set; }
public DbSet<List<String>> Category { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MarketPresence>().HasNoKey().ToView(null);
modelBuilder.Entity<Campaign>().HasNoKey().ToView(null);
modelBuilder.Entity<List<String>>().HasNoKey().ToView(null);
}
}
关于我MarketPresence
和Campaign
班级,当我像这样使用它们时一切正常:
public IEnumerable<Campaign> GetCampaignReports(Campaign input)
{
var result = _reportContext
.Campaign
.FromSqlRaw("GetCampaignReport " +
$"{input.ProvinceId}, " +
$"{input.CityId}")
}
但关于最后一种情况Category
,我只想返回字符串列表作为我的 sql 查询的结果,我收到这条消息的异常:(The required column 'Capacity' was not present in the results of a 'FromSql' operation.
容量是我的 sql 结果查询的名称,查询的结果是一个表列名为容量)我怎么能想出这个问题?
感谢您的关注。
解决方案
我有类似的东西,只是做了一个像你的 Category 类的类,它有数百个属性。这是我能想到的最简单的方法。
推荐阅读
- javascript - 如何使用字符串名称访问数组 (Javascript)
- javascript - 了解 Vuejs 中属性的执行顺序
- sql - 使用 sequelize 计算 where 查询编码
- python - df。drop 导致 &: 'float' 和 'bool' 的操作数类型不受支持
- javascript - 在滚动时隐藏导航栏不会使导航栏在向上滚动后固定到位
- ios - UNUserNotificationCenterDelegate didReceive 响应没有被调用
- shopify - 如何在自定义 shopify 部分中添加 url 选项
- javascript - 如何检查 AJAX 请求的成功/错误回调?
- excel - 将文本添加到消息框输出
- sql - Spring data jpa for between search:区别Between还是IsBetween?