'FromSql' 操作的结果中不存在所需的列,[NotMapped] 没有帮助

The required column was not present in the results of a 'FromSql' operation and [NotMapped] has not helped

我正在使用 EF Core 3.0 从 SQL 服务器存储过程中获取数据到我名为 Recipient 的对象模型中。 Recipient 模型定义了两个属性,EnvelopeIdName,它们不是从存储过程返回的。当我使用 FromSqlInterpolated 调用存储过程时,我收到错误消息

The required column 'EnvelopeId1' was not present in the results of a 'FromSql' operation

根据我在 EF Core 文档中阅读的内容,我应该能够向这些属性添加 [NotMapped] 属性,并且 EF Core 应该能够在读取或写入数据库时​​忽略它们,正确的?不幸的是,这对我不起作用,我收到了上述错误。

如果我在存储过程中添加两列并删除对象模型中的 [NotMapped] 属性,一切正常。这证明我的其他 column/property 名称正确匹配并且没有任何拼写错误。

我看到了使用 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 正在创建影子 属性,因为它识别出 RecipientEnvelope 对象的子对象.老实说,我真的 应该 在结果集中包含 EnvelopeId(并删除 [NotMapped])以安抚 EF Core。我没有在存储过程的结果集中返回 EnvelopeId 的唯一原因是因为我一开始就将它作为输入参数传入,并认为传入一些东西然后得到它会浪费网络资源它返回到每个结果集记录中。

现在我不知道 EnvelopeId1 是从哪里来的,但这就是错误消息中提到的内容。正如您在我的对象模型和存储过程结果集架构中看到的那样,我只引用了 EnvelopeId。我最好的猜测是,当 EF Core 决定制作影子 属性 时,它无法使用 EnvelopeId,因为我的对象模型中已经有了它,所以它创建了一个名为 EnvelopeId1 和然后期望它出现在存储过程的结果集中。

出现此错误消息的原因有两个:

The required column 'EnvelopeId1' was not present in the results of a 'FromSql' operation

很明显,对于 SQL 结果集中的每一列,对象图中必须有一个对应的 属性 是您尝试水合的,在本例中是您的 Recipient class 没有 EnvelopeId1.

因为您的查询是一个存储过程,所以 post SQL 会导致这样的问题,因为它会显示您尝试的 SQL 结构映射到您的对象图。

EnvelopeId1 在您的 SQL table 中但不在您的对象图中的原因是 Entity Framework 已经为 Shadow Property for the foreign that you have not defined a property 创建了一个.

你还没有列出 Envelope 的架构,但我的钱说你有一个等效的 ICollection<Recipient> 属性.

EnvelopeId 创建 NotMapped 属性 只会让这个模式更加混乱,我们应该正确映射它,或者你应该从你的存储过程输出中省略它。

If you are going to use FromSql variants in EF (.Net or Core) then you should always define navigation properties in the schema classes as Shadow or Auto foreign key columns in the database will cause trouble unless you explicitly omit the columns that exist in the database but not in your schema

This means no SELECT * you will have to explicitly define all columns.

方案一:修改存储过程
您可以简单地将您的存储过程修改为不是来自您的收件人 table 的 return EvelopeId1

解决方案 2:为数据中所有关系的两端完全定义属性 classes
在这里,我使用 EnvelopeId1 作为名称,因此不需要迁移,但是我建议您将其更改为 EnvelopeId,我个人使用数据库中以数字结尾的任何字段的存在作为表明我的一些关系没有被充分定义。

public class Recipient
{
    [Key]
    public long RecipientId { get; set; }
    public Guid? EnvelopeId1 { get; set; }
    [ForeignKey(nameof(EnvelopeId1))]
    public virtual Envelope Envelope { 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() { }
}

这是基于以下假设:您的信封 class 的定义类似于此部分定义:

public class Envelope
{
    [Key]
    public Guid EnvelopeId { get; set; }
    public virtual ICollection<Recipient> Recipients { get; set; } = new Hashset<Recipient>();
    ...
}