我正在尝试从EF Core调用存储过程,并且遇到了一个我不知道如何解决的错误。
I am trying to call a stored procedure from EF Core, and am running into an error I don't know how to solve.
类:
public class PlayerOverview { public string Player { get; set; } public int World { get; set; } public string Alliance { get; set; } public int Score { get; set; } public int Castles { get; set; } public int Cities { get; set; } }查询:
_context.Set<PlayerOverview>() .FromSql($"CALL usp_player_overview('{player}')") .Select(pl => new { Player = pl.Player, World = pl.World, Alliance = pl.Alliance, Score = pl.Score, Castles = pl.Castles, Cities = pl.Cities });例外情况:
抛出异常: Microsoft.EntityFrameworkCore.dll中的'System.InvalidOperationException'
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll
其他信息:无法创建'PlayerOverview'的DbSet,因为此类型不包含在上下文模型中。
Additional information: Cannot create a DbSet for 'PlayerOverview' because this type is not included in the model for the context.
如何包含此类型类在上下文中的模型?如果我添加 DbSet< PlayerOverview> ,则会收到另一个异常,抱怨该集合缺少主键。
How can I include this class in the model for the context? If I add a DbSet<PlayerOverview> I get a different exception complaining about a lack of primary key for the set.
推荐答案您需要添加一个DbSet并定义一个主键,因为没有名为Id或PalyerOverviewId的列,EF无法猜测哪个列是键。因此,请使用[Key]属性或流利的api(或仅在结果和模型中添加唯一的PlayerOverviewId:
You need to add a DbSet, and define a primary key, as there are no columns called Id or PalyerOverviewId, EF is unable to guess which column is the key. So use either the [Key] attribute or the fluent api (or simply add a unique PlayerOverviewId to your result and model:
[Key] public string Player { get; set; }docs.microsoft/en-us/ef/core/modeling/keys
更多推荐
无法在EF Core中使用FromSql调用存储过程
发布评论