【发布时间】:2021-07-05 20:42:43
【问题描述】:
我正在尝试使用实体框架在 ASP.NET MVC 中执行存储过程。存储过程在返回的数据行中不返回任何唯一键。
我收到此错误:
类型:System.Data.Entity.ModelConfiguration.ModelValidationException, EntityFramework, Version=6.0.0.0
消息:在模型生成过程中检测到一个或多个验证错误:
SPProject.DTO.Result: : EntityType 'Result' 没有定义键。定义此 EntityType 的键。
结果:EntityType:EntitySet 'Results' 基于没有定义键的类型 'Result'。
实体类:
public class Result
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public string prop3 { get; set; }
public string prop4 { get; set; }
}
数据库上下文:
public class ResultDBContext : DbContext
{
public DbSet<Result> Results { get; set; }
}
控制器:
using (var context = new ResultDBContext())
{
var input1 = new SqlParameter("@q1", q1);
var input2 = new SqlParameter("@q2", q2);
var input3 = new SqlParameter("@q3", "%");
var result = context.Results
.SqlQuery("[dbo].[sp] @q1, @q2, @q3", input1, input2, input3)
.ToList();
}
我读到了关于使用复杂类型而不是实体来映射没有键的存储过程的输出,但不知道如何做到这一点。
【问题讨论】:
标签: c# asp.net-mvc entity-framework