我正在使用EF Core 3.0将数据从SQL Server存储过程提取到名为Recipient
的对象模型中。 Recipient
模型具有定义的两个属性EnvelopeId
和Name
,这些属性未从存储过程中返回。当我使用FromSqlInterpolated
调用存储过程时,收到错误消息
'FromSql'操作的结果中没有所需的列'EnvelopeId1'
根据我在EF Core文档中所读的内容,我应该能够向这些属性添加[NotMapped]
属性,并且EF Core在读取或写入数据库时应该能够忽略它们,对吗?不幸的是,这对我不起作用,并且出现了以上错误。
如果我在存储过程中添加了两列,并在对象模型中删除了[NotMapped]
属性,则一切正常。这证明我的其他列/属性名称正确匹配,并且在任何地方都没有错字。
我已经看到使用DbQuery
而不是DbSet
的建议,但是已经过时了,所以我不想使用它。我已经看到了使用两种不同对象模型的建议,一种具有与存储过程结果集完全匹配的属性,但这只会导致很多额外的模型。我在这里做什么错了?
我的对象模型:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyNamespace.Models
{
public class Recipient
{
[Key]
public long RecipientId { get; set; }
[NotMapped]
public Guid? EnvelopeId { get; set; }
public string Type { get; set; }
public string UserId { get; set; }
public string Email { get; set; }
[NotMapped]
public string Name { get; set; }
public string RoleName { get; set; }
public int RoutingOrder { get; set; }
public string Status { get; set; }
public DateTime StatusDate { get; set; }
public Recipient() { }
}
}
我的数据库上下文:
using microsoft.EntityFrameworkCore;
using MyNamespace.Models;
namespace MyNamespace.Data
{
public class MyDbContext : DbContext
{
public virtual DbSet<Recipient> Recipient { get; set; }
public virtual DbSet<Envelope> Envelope { get; set; }
public virtual DbSet<Template> Template { get; set; }
public MyDbContext (DbContextOptions<MyDbContext> options) : base(options)
{ }
}
}
我的失败代码:
FormattableString sql = $"EXEC dbo.MyStoredProcedure @Param1={param1},@Param2={param2}";
var result = await MyDbContext.Recipient.FromSqlInterpolated(sql).ToListAsync();
编辑#1 ...
Envelope
对象模型:
public class Envelope
{
[Key]
public Guid EnvelopeId { get; set; }
public string Status { get; set; }
public DateTime StatusDate { get; set; }
public DateTime StatusPollDate { get; set; }
[NotMapped]
public List<Recipient> Recipients { get; set; }
public Envelope() {
Recipients = new List<Recipient>();
StatusPollDate = DateTime.Parse("1753-01-01");
}
}
存储过程结果集架构:
[RecipientId] bigint,[Type] varchar(20),[UserId] varchar(50),[Email] varchar(100),[RoleName] varchar(100),[RoutingOrder] int,[Status] varchar(13),[StatusDate] datetime
在阅读了一些答案和评论后,我意识到EF正在创建shadow属性,因为它认识到Recipient
是Envelope
对象的子级。坦白地说,我真的应该在结果集中添加EnvelopeId
(并删除[NotMapped]
)以安抚EF Core。我没有在存储过程的结果集中返回EnvelopeId
的唯一原因是因为我已经将它作为输入参数进行了传递,并且认为这浪费了网络资源来传递某些东西然后得到并返回到每个结果集记录。
现在我不知道EnvelopeId1
的来源,但这就是错误消息所提及的内容。如您在我的对象模型和存储过程结果集模式中所看到的,我只引用过EnvelopeId
。我最好的猜测是,当EF Core决定创建shadow属性时,由于我已经在对象模型中拥有它,所以它无法使用EnvelopeId
,因此它创建了一个名为EnvelopeId1
的对象,然后期望它可以出现在存储过程的结果集中。