从实体框架中接收数据

发布于 2025-01-19 16:23:21 字数 4698 浏览 0 评论 0原文

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 Server 中的过程 - 它有效 - 我从 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:序列不包含任何元素 System.Linq.ThrowHelper.ThrowNoElementsException() 在 System.Linq.Enumerable.Max(IEnumerable`1 源) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitBinary(BinaryExpression 二进制表达式) at System.Linq.Expressions.ExpressionVisitor.Visit(表达式节点)位于 System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor 访问者,BlockExpression 块)位于 System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression 节点)在 System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor 访问者)在 System.Linq.Expressions.ExpressionVisitor.Visit(表达式 节点)在 Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(表达式 扩展表达式) at System.Linq.Expressions.Expression.Accept(ExpressionVisitor 访客) 在 System.Linq.Expressions.ExpressionVisitor.Visit(表达式节点)处 Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(表达式 shaperExpression、RelationalCommandCache&关系命令缓存, Lambda表达式&相关DataLoaders、Int32&集合Id)位于

有人知道如何做到这一点吗?我应该走另一条路吗?

请检查一下我所做的是否有意义,或者走更好的路不是更好吗?

Database in Oracle - View:

     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)

Procedure in SQL Server - it works - I get the data from 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

Model

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; }
}

Context

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 -- HERE I HAVE ERROR

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;
}

Error:

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

Does anyone have an idea how to do this? Should I go another way?

Please check if what I am doing has any sense, or is it not better to go a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葮薆情 2025-01-26 16:23:21

EF Core无法创建投影表达式,因为ContractNumber的所有属性均已阅读。添加设定器来解决您的问题:

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; }
}

EF Core cannot create projection expression because all properties of ContractNumber are readonly. Add setters to solve your issue:

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文