我有一个大问题FromSql
就是这样:
我有一个这样的模型:
public partial class model
{
public string name
}
我想从数据库(sql server)中的过程中获得一些结果。
当我执行下面的代码时
var sql = "EXECUTE [myprocedure] @param1 ";
SqlParameter sqlParameter = new SqlParameter
{
ParameterName = "param1",
DbType = DbType.Int32,
Value = 10;
}
var result = db.model.FromSql(sql,SqlParameter);
它显示这样的异常:The entity type 'model' requires a primary key to be defined.
所以我将主键添加到我的模型中:
public partial class model
{
[key]
public int ID {set;get;}
public string name
}
但这次它显示了这个异常:The required column 'ID' was not present in the results of a 'FromSql' operation.
我知道我必须添加ID
我的数据库响应,但我不能这样做,因为我的数据库中有很多程序,所以我无法编辑所有这些程序。
所以我正在寻找一种方法来解决我的问题而无需编辑我的程序。
有人可以帮助我吗?
如果您使用的是 Entity Framework Core 2.x,请查看查询类型(https://learn.microsoft.com/en-us/ef/core/modeling/query-types https://learn.microsoft.com/en-us/ef/core/modeling/query-types)。
表(实体类型)始终需要 ID,而查询类型则不需要。
对于查询类型,您可以省略 ID。否则,您必须确保您的存储过程也返回一个 ID,因为表/实体类型始终需要一个 ID。
从 EF Core 3.0 开始,有一个 .HasNoKey() 而不是查询类型来定义没有 ID 的实体(https://learn.microsoft.com/de-de/ef/core/what-is-new/ef-core-3.0/writing-changes#query-types-are-consolidated-with-entity-types https://learn.microsoft.com/de-de/ef/core/what-is-new/ef-core-3.0/breaking-changes#query-types-are-consolidated-with-entity-types).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)