简体   繁体   English

如何在实体框架中调用带有联接的存储过程?

[英]How to call Stored Procedure with Joins in Entity framework?

.Net core 3.1 .Net 核心 3.1

This is my tables这是我的桌子

I am not allowed to add any foreign keys or new relation into them.我不允许在其中添加任何外键或新关系。 I made a stored procedure that joins other 3 tables to Ordering_Policy table.我制作了一个将其他 3 个表连接到 Ordering_Policy 表的存储过程。

create procedure spOrderingPolicy
as
    select op.Id, op.Tyre_Grade, op.Tyre_Group, tb.Tyre_Brand, ss.Status, tc.Cosmetic
    from [dbo].[Ordering_Policy] as op

    left join [dbo].[Tyre_Brand] as tb
    on op.Tyre_Brand_Id = tb.Id
    left join [dbo].[Shearography_Status] as ss
    on op.Shearography_Id = ss.Id
    left join [dbo].[Tyre_Cosmetic] as tc
    on op.Cosmetic_Id = tc.Id
go

The result result is as expected:结果结果如预期:

So I tried to call the procedure:所以我尝试调用该程序:

public IEnumerable<OrderingPolicy> GetOrder()
{
    using (Ordering_PolicyContext context = new Ordering_PolicyContext())
    {
        var order = context.OrderingPolicies.FromSqlRaw<OrderingPolicy>("spOrderingPolicy").ToList();
        return order;
    }
}

But it ignores anything not in the Ordering_Policy: result但它忽略了 Ordering_Policy 中没有的任何内容:结果

How do I call the procedure and displays all the data like in the query?如何调用该过程并显示查询中的所有数据?

Edit: This is the class for OrderingPolicy编辑:这是用于 OrderingPolicy 的 class

namespace Ordering_Policy.Models
{
    public partial class OrderingPolicy
    {
        public int Id { get; set; }
        public string TyreGrade { get; set; }
        public int? TyreGroup { get; set; }
        public int? ShearographyId { get; set; }
        public string CasingLife { get; set; }
        public string Variance { get; set; }
        public string ColorCode { get; set; }
        public string Injury { get; set; }
        public int? MaxCrownPatch { get; set; }
        public int? MaxShoulder { get; set; }
        public int? Age { get; set; }
        public int? CosmeticId { get; set; }
        public int? TyreBrandId { get; set; }
    }
}

You need to create a Keyless Entity Type that matches the resultset returned by your stored procedure, and call OrderingPolicyEx.FromSqlRaw on that DbSet and not OrderingPolicy .您需要创建一个与存储过程返回的结果集匹配的无键实体类型,并在该 DbSet 而不是OrderingPolicy OrderingPolicyEx.FromSqlRaw

You should use INNER Join rather than left Join:您应该使用INNER Join而不是 left Join:

This is My demo,with your entity class(a little simplified):这是我的演示,带有您的实体类(有点简化):

CREATE PROCEDURE [dbo].[spOrderingPolicy]  
as

select op.Id, op.TyreGrade, 
op.TyreGroup,op.CosmeticId,op.ShearographyId,op.TyreBrandId, tb.Brand, ss.status, 
tc.cosmetic
from dbo.OrderingPolicy as op

INNER JOIN [dbo].[Tyre_Brand] as tb
on op.TyreBrandId = tb.Id
INNER JOIN [dbo].[Shearography_Status] as ss
on op.ShearographyId = ss.Id
INNER JOIN [dbo].[Tyre_Cosmetic] as tc
on op.CosmeticId = tc.Id
Go

Result:only matched rows will be showed结果:只显示匹配的行

在此处输入图像描述

I finally able to find the solution: Make new class and context solely to call the columns from the stored procedure我终于找到了解决方案:制作新的 class 和上下文,仅用于调用存储过程中的列

Class: Class:

public partial class Tyre_Detail
{
    public int? Id { get; set; }
    public string Tyre_Grade { get; set; }
    public int? Tyre_Group { get; set; }
    public string Tyre_Brand { get; set; }
    public string Status { get; set; }
    public string Cosmetic { get; set; }
}

The data must have the same name exactly like the columns from the stored procedure.数据必须具有与存储过程中的列完全相同的名称。 There are no keys in this class so the data type, in this case is int needs to have question mark.这个 class 中没有键,所以数据类型,在这种情况下是int需要有问号。

Then create the context file for the class:然后为 class 创建上下文文件:

modelBuilder.Entity<Tyre_Detail>(entity =>
{
     entity.HasNoKey();

     entity.Property(e => e.Id);
     entity.Property(e => e.Tyre_Grade);
     entity.Property(e => e.Tyre_Group);
     entity.Property(e => e.Tyre_Brand);
     entity.Property(e => e.Status);
     entity.Property(e => e.Cosmetic);
     });

Since this is keyless entities, .HasNoKey() is essential.由于这是无密钥实体,因此 .HasNoKey() 是必不可少的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM