从 Entity Framework 接收数据的执行程序
Exec Procedure who receive data from Entity Framework
Oracle 中的数据库 - 视图:
COLUMN_NAME | DATE_TYPE
___________________________________________________
UMOWA | VARCHAR2(30)
RODZ_LEAS | VARCHAR2(1)
KTH | NUMBER(10)
SKRÓT | VARCHAR2(50)
NAZWA | VARCHAR2(240)
AMORTYZ | NUMBER(10,6)
WALUTA | VARCHAR2(3)
WARTOSC_UMW | NUMBER
LICZBA_RAT | NUMBER
WK | NUMBER
WK% | NUMBER
Rodzaj stopy baz.| VARCHAR2(2000)
Wart.stopy baz. | NUMBER
REFI | NUMBER
RA_NR_RATY | NUMBER(10)
RA_TYP | VARCHAR2(10)
RA_DATA_SPLATY | DATE
RA_KWOTA_NETTO | NUMBER(12,2)
RA_KAPITAL_POZOSTALY | NUMBER(12,2)
SQL 服务器中的程序 - 它有效 - 我从 Oracle 获取数据。
CREATE PROCEDURE [egeria].[ContractData_P] (@ContractNumberPar varchar(20))
AS
EXECUTE(
'Select
Umowa ContractNumber
, Rodz_leas TypeOfLeasing
, Kth CodeClient
, skrót Short
, Nazwa NameOfClient
, amortyz Amortization
, Waluta Currency
, Wartosc_Umw ContractValue
, Liczba_rat NumberOfInstallments
, WK FinalValue
, "WK%" FinalValuePercent
, "Rodzaj stopy baz." TypeOfBaseRate
, "Wart.stopy baz." ValueOfBaseRate
, REFI
, ra_nr_raty InstallmentNumber
, ra_typ Type
, ra_data_splaty MaturityDate
, ra_kwota_netto NetAmount
, ra_kapital_pozostaly CapitalRemaining
from RAPADM.XLS_SKROCENIA_UMOW_V
where UMOWA = ?'
, @ContractNumberPar) AT EF_EG5PROD
GO
型号
public class ContractData
{
public string ContractNumber { get; }
public string TypeOfLeasing { get; }
public int CodeClient { get; }
public string Short { get; }
public string NameOfClient { get; }
public decimal Amortization { get; }
public string Currency { get; }
public decimal ContractValue { get; }
public decimal NumberOfInstallments { get; }
public decimal FinalValue { get; }
public decimal FinalValuePercent { get; }
public string TypeOfBaseRate { get; }
public decimal ValueOfBaseRate { get; }
public decimal REFI { get; }
public decimal InstallmentNumber { get; }
public string Type { get; }
public DateTime MaturityDate { get; }
public decimal NetAmount { get; }
public decimal CapitalRemaining { get; }
}
上下文
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public virtual DbSet<ContractData> ContractData { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ContractData>().HasNoKey();
}
}
ContractDataService -- 这里我有错误
private readonly AppDbContext _context;
public ContractDataService(AppDbContext context)
{
_context = context;
}
public async Task<IEnumerable<ContractData>> ExecAsync(string contractNumberPar)
{
var data = _context.ContractData.FromSqlRaw("EXECUTE [egeria].[ContractData_P] @ContractNumberPar", contractNumberPar).AsEnumerable();
return data;
}
错误:
system.InvalidOperationException: Sequence contains no elements at
System.Linq.ThrowHelper.ThrowNoElementsException() at
System.Linq.Enumerable.Max(IEnumerable`1 source) at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitBinary(BinaryExpression
binaryExpression) at
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at
System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor
visitor, BlockExpression block) at
System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression
node) at
System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor
visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node) at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression
extensionExpression) at
System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression
shaperExpression, RelationalCommandCache& relationalCommandCache,
LambdaExpression& relatedDataLoaders, Int32& collectionId) at
有没有人知道如何做到这一点?我应该走另一条路吗?
请问我这样做有没有意义,还是走更好的路不是更好吗?
EF Core 无法创建投影表达式,因为 ContractNumber
的所有属性都是只读的。添加 setter 来解决您的问题:
public class ContractData
{
public string ContractNumber { get; set; }
public string TypeOfLeasing { get; set; }
public int CodeClient { get; set; }
public string Short { get; set; }
public string NameOfClient { get; set; }
public decimal Amortization { get; set; }
public string Currency { get; set; }
public decimal ContractValue { get; set; }
public decimal NumberOfInstallments { get; set; }
public decimal FinalValue { get; set; }
public decimal FinalValuePercent { get; set; }
public string TypeOfBaseRate { get; set; }
public decimal ValueOfBaseRate { get; set; }
public decimal REFI { get; set; }
public decimal InstallmentNumber { get; set; }
public string Type { get; set; }
public DateTime MaturityDate { get; set; }
public decimal NetAmount { get; set; }
public decimal CapitalRemaining { get; set; }
}
Oracle 中的数据库 - 视图:
COLUMN_NAME | DATE_TYPE
___________________________________________________
UMOWA | VARCHAR2(30)
RODZ_LEAS | VARCHAR2(1)
KTH | NUMBER(10)
SKRÓT | VARCHAR2(50)
NAZWA | VARCHAR2(240)
AMORTYZ | NUMBER(10,6)
WALUTA | VARCHAR2(3)
WARTOSC_UMW | NUMBER
LICZBA_RAT | NUMBER
WK | NUMBER
WK% | NUMBER
Rodzaj stopy baz.| VARCHAR2(2000)
Wart.stopy baz. | NUMBER
REFI | NUMBER
RA_NR_RATY | NUMBER(10)
RA_TYP | VARCHAR2(10)
RA_DATA_SPLATY | DATE
RA_KWOTA_NETTO | NUMBER(12,2)
RA_KAPITAL_POZOSTALY | NUMBER(12,2)
SQL 服务器中的程序 - 它有效 - 我从 Oracle 获取数据。
CREATE PROCEDURE [egeria].[ContractData_P] (@ContractNumberPar varchar(20))
AS
EXECUTE(
'Select
Umowa ContractNumber
, Rodz_leas TypeOfLeasing
, Kth CodeClient
, skrót Short
, Nazwa NameOfClient
, amortyz Amortization
, Waluta Currency
, Wartosc_Umw ContractValue
, Liczba_rat NumberOfInstallments
, WK FinalValue
, "WK%" FinalValuePercent
, "Rodzaj stopy baz." TypeOfBaseRate
, "Wart.stopy baz." ValueOfBaseRate
, REFI
, ra_nr_raty InstallmentNumber
, ra_typ Type
, ra_data_splaty MaturityDate
, ra_kwota_netto NetAmount
, ra_kapital_pozostaly CapitalRemaining
from RAPADM.XLS_SKROCENIA_UMOW_V
where UMOWA = ?'
, @ContractNumberPar) AT EF_EG5PROD
GO
型号
public class ContractData
{
public string ContractNumber { get; }
public string TypeOfLeasing { get; }
public int CodeClient { get; }
public string Short { get; }
public string NameOfClient { get; }
public decimal Amortization { get; }
public string Currency { get; }
public decimal ContractValue { get; }
public decimal NumberOfInstallments { get; }
public decimal FinalValue { get; }
public decimal FinalValuePercent { get; }
public string TypeOfBaseRate { get; }
public decimal ValueOfBaseRate { get; }
public decimal REFI { get; }
public decimal InstallmentNumber { get; }
public string Type { get; }
public DateTime MaturityDate { get; }
public decimal NetAmount { get; }
public decimal CapitalRemaining { get; }
}
上下文
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public virtual DbSet<ContractData> ContractData { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ContractData>().HasNoKey();
}
}
ContractDataService -- 这里我有错误
private readonly AppDbContext _context;
public ContractDataService(AppDbContext context)
{
_context = context;
}
public async Task<IEnumerable<ContractData>> ExecAsync(string contractNumberPar)
{
var data = _context.ContractData.FromSqlRaw("EXECUTE [egeria].[ContractData_P] @ContractNumberPar", contractNumberPar).AsEnumerable();
return data;
}
错误:
system.InvalidOperationException: Sequence contains no elements at System.Linq.ThrowHelper.ThrowNoElementsException() at System.Linq.Enumerable.Max(IEnumerable`1 source) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor visitor, BlockExpression block) at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node) at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression) at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders, Int32& collectionId) at
有没有人知道如何做到这一点?我应该走另一条路吗?
请问我这样做有没有意义,还是走更好的路不是更好吗?
EF Core 无法创建投影表达式,因为 ContractNumber
的所有属性都是只读的。添加 setter 来解决您的问题:
public class ContractData
{
public string ContractNumber { get; set; }
public string TypeOfLeasing { get; set; }
public int CodeClient { get; set; }
public string Short { get; set; }
public string NameOfClient { get; set; }
public decimal Amortization { get; set; }
public string Currency { get; set; }
public decimal ContractValue { get; set; }
public decimal NumberOfInstallments { get; set; }
public decimal FinalValue { get; set; }
public decimal FinalValuePercent { get; set; }
public string TypeOfBaseRate { get; set; }
public decimal ValueOfBaseRate { get; set; }
public decimal REFI { get; set; }
public decimal InstallmentNumber { get; set; }
public string Type { get; set; }
public DateTime MaturityDate { get; set; }
public decimal NetAmount { get; set; }
public decimal CapitalRemaining { get; set; }
}