列映射到未命名列
Column mapping to unnamed columns
我正在使用 Entity Framework Core 3 来尝试执行存储过程。我需要获取存储过程的 return 值,但是,在存储过程中,没有名称。这是我执行存储过程的代码:
var data = await _context.Set<ArfmCreateEditRequestDto>()
.FromSqlRaw(@"ARFMCreateEditRequest {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25}",
"Add", accountType, srIdentifier, srCheckDigit, arIdentifier, arCheckDigit, customerName, changeCustomerName,
supplierId, amount, paymentMethod, addressType, address1, address2, address3, address4, country, city,
state, zip, statusCode, createdBy, invoiceDate, approvedBy, comments, updateId).ToListAsync();
这是我的 ArfmCreateEditRequestDto
class:
public class ArfmCreateEditRequestDto
{
[Column(Order = 0)]
public string Retval { get; set; }
}
当我在 SQL 服务器上执行此操作时,我得到这些结果:
--------------------------------------
| (No column name) |
--------------------------------------
| RequestID: 36 Succesfully Added |
--------------------------------------
当我 运行 我的程序时,我得到一个异常
"System.InvalidOperationException: The required column 'Retval' was not present in the results of a 'FromSql' operation."
如果我改成这样:
[Column("", Order = 0)]
public string Retval { get; set; }
然后我得到这个异常:
"System.ArgumentException: The argument 'name' cannot be null, empty or contain only whitespace. (Parameter 'name')"
我真的认为第一件事会起作用,Entity Framework 只会将订单位置 0 的数据映射到我的 属性,但显然它没有。
所以我的问题是,如果列在存储过程中未命名,如何捕获我的 return 值?请注意,我根本无法更改存储过程。
这不可能。使用中有一些限制 FromSqlRaw
1-The SQL query must return data for all properties of the entity type.
2-The column names in the result set must match the column names that properties are mapped to. Note this behavior is different from EF6. EF6 ignored property to column mapping for raw SQL queries and result set column names had to match the property names
基于此link:https://docs.microsoft.com/en-us/ef/core/querying/raw-sql
但我认为您可以包装存储过程并将结果插入到具有任意列的临时 table 中:
var data = await _context.Set<ArfmCreateEditRequestDto>()
.FromSqlRaw(@"create table #tmp( Retval nvarchar(128) ) insert into #tmp( Retval ) exec ARFMCreateEditRequest {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25} select Retval from #tmp",
"Add", accountType, srIdentifier, srCheckDigit, arIdentifier, arCheckDigit, customerName, changeCustomerName,
supplierId, amount, paymentMethod, addressType, address1, address2, address3, address4, country, city,
state, zip, statusCode, createdBy, invoiceDate, approvedBy, comments, updateId).ToListAsync();
我正在使用 Entity Framework Core 3 来尝试执行存储过程。我需要获取存储过程的 return 值,但是,在存储过程中,没有名称。这是我执行存储过程的代码:
var data = await _context.Set<ArfmCreateEditRequestDto>()
.FromSqlRaw(@"ARFMCreateEditRequest {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25}",
"Add", accountType, srIdentifier, srCheckDigit, arIdentifier, arCheckDigit, customerName, changeCustomerName,
supplierId, amount, paymentMethod, addressType, address1, address2, address3, address4, country, city,
state, zip, statusCode, createdBy, invoiceDate, approvedBy, comments, updateId).ToListAsync();
这是我的 ArfmCreateEditRequestDto
class:
public class ArfmCreateEditRequestDto
{
[Column(Order = 0)]
public string Retval { get; set; }
}
当我在 SQL 服务器上执行此操作时,我得到这些结果:
--------------------------------------
| (No column name) |
--------------------------------------
| RequestID: 36 Succesfully Added |
--------------------------------------
当我 运行 我的程序时,我得到一个异常
"System.InvalidOperationException: The required column 'Retval' was not present in the results of a 'FromSql' operation."
如果我改成这样:
[Column("", Order = 0)]
public string Retval { get; set; }
然后我得到这个异常:
"System.ArgumentException: The argument 'name' cannot be null, empty or contain only whitespace. (Parameter 'name')"
我真的认为第一件事会起作用,Entity Framework 只会将订单位置 0 的数据映射到我的 属性,但显然它没有。
所以我的问题是,如果列在存储过程中未命名,如何捕获我的 return 值?请注意,我根本无法更改存储过程。
这不可能。使用中有一些限制 FromSqlRaw
1-The SQL query must return data for all properties of the entity type.
2-The column names in the result set must match the column names that properties are mapped to. Note this behavior is different from EF6. EF6 ignored property to column mapping for raw SQL queries and result set column names had to match the property names
基于此link:https://docs.microsoft.com/en-us/ef/core/querying/raw-sql
但我认为您可以包装存储过程并将结果插入到具有任意列的临时 table 中:
var data = await _context.Set<ArfmCreateEditRequestDto>()
.FromSqlRaw(@"create table #tmp( Retval nvarchar(128) ) insert into #tmp( Retval ) exec ARFMCreateEditRequest {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25} select Retval from #tmp",
"Add", accountType, srIdentifier, srCheckDigit, arIdentifier, arCheckDigit, customerName, changeCustomerName,
supplierId, amount, paymentMethod, addressType, address1, address2, address3, address4, country, city,
state, zip, statusCode, createdBy, invoiceDate, approvedBy, comments, updateId).ToListAsync();