我可以根据我调用的存储过程的类型在运行时更改数据注释吗

Can I change Data Annottion at Runtime based on the type of storedprocedure I am calling

嗨,我的实体是 ParameterDetail,如下所示:

public class ParameterDetail
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    [NotMapped]
    public string Description { get; set; }
    //..other columns removed for brevity
    public int LookupValueId { get; set; }
}

我调用我的存储过程并加载结果如下...

        List<ParameterDetail> paramDetails = this.ParameterDetails.FromSqlRaw("EXEC dbo.GE_GetStartParameter @GuidelineName={0}", guidelineName).ToList();

现在一切正常,但现在我必须调用一个略有不同的过程,它没有 LookupValueId..

List<ParameterDetail> paramDetails =this.ParameterDetails.FromSqlRaw("EXEC dbo.GetParameterDetails @ParameterId={0}", nextParam).ToList();

我不想只为这一列添加另一个 EntityModel.... 我可以在运行时以某种方式使用 Mapped 属性 (数据注释)吗?或者还有其他解决办法吗?

Can i use Mapped property (Data Annotation) at runtime somehow? Or could there be any other solution?

如果您希望 EF 执行映射,则不是,至少不是一个好的映射。您无法在运行时更改属性,但如果您使用 Fluent 配置,则可以有两个 不同的 DbContext 子类型,它们以不同的方式配置同一实体 class。但这不是一个非常优雅的解决方案。

您始终可以使用 ADO.NET 执行存储过程并根据需要映射数据。