gpt4 book ai didi

entity-framework-core - 如何将 'FromSql()' 查询的结果建模为基本数据类型?

转载 作者:行者123 更新时间:2023-12-04 03:59:35 24 4
gpt4 key购买 nike

<分区>

我正在使用 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);
}
}

关于我的 MarketPresenceCampaign 类,当我像这样使用它们时一切正常:

public IEnumerable<Campaign> GetCampaignReports(Campaign input)
{
var result = _reportContext
.Campaign
.FromSqlRaw("GetCampaignReport " +
$"{input.ProvinceId}, " +
$"{input.CityId}")
}

但关于最后一个案例 Category,我只想返回字符串列表作为我的 sql 查询的结果,我得到异常消息:The required column 'Capacity'不存在于“FromSql”操作的结果中。(容量是我的 sql 结果查询的名称,查询的结果是一个具有名为容量的单列的表)我怎么能想到这个问题?

感谢您的关注。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com