簡體   English   中英

如何在 Entity Framework Data First 中使用 FromSqlRaw 引用查找表中的字段?

[英]How do I reference fields from a lookup table using FromSqlRaw in Entity Framework Data First?

我在這里查看了許多看起來相似的文章,但沒有一篇討論使用dbSetFromSqlRaw方法從多個表(例如查找表)返回數據的主題。

有問題的表 - SlotDataSlotdenom 使用外鍵在 SQL 數據庫中相關,我使用 NuGet 生成數據模型。controller 的Details()任務導致有用數據開始發送到視圖,但Index()任務沒有。

Index方法是我的dbSet.FromSqlRaw所在的位置。

DbContext文件:

modelBuilder.Entity<SlotDatum>(entity =>
{
    entity.HasKey(e => e.Id).HasName("PK_Slot_Data_1");
    entity.ToTable("Slot_Data");
    entity.HasIndex(e => e.Id, "IX_Slot_Data");

    entity.Property(e => e.CalDate)
        .HasColumnType("date")
        .HasColumnName("Cal_Date");
    entity.Property(e => e.Denom_Id).HasColumnName("Denom_ID");
    entity.Property(e => e.Drop).HasColumnType("money");
    entity.Property(e => e.Handle).HasColumnType("money");
    entity.Property(e => e.Win).HasColumnType("money");

    entity.Property(d => d.Denom_Id).HasColumnType("int");
    //entity.Property(e => e.Description).HasColumnName("Description");

    entity.HasOne(d => d.Denom).WithMany(p => p.SlotData)
        .HasForeignKey(d => d.Denom_Id)
        .HasConstraintName("FK_Slot_Data_SlotDenom");
});

SlotsDatum model class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace FlashWeb.Models.DB;

public partial class SlotDatum
{
    [Key]
    public int Id { get; set; }

    // public DateTime? CalDate { get; set; }
    public DateOnly CalDate { get; set; }

    public int? Denom_Id { get; set; }
    public decimal? Handle { get; set; }
    public decimal? Drop { get; set; }
    public decimal? Win { get; set; }
    public int? SlotCount { get; set; }

    public virtual SlotDenom? Denom { get; set; }
}

SlotDenom model class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace FlashWeb.Models.DB;

public partial class SlotDenom
{
    [Key]
    public int Denom_Id { get; set; }

    public decimal? Value { get; set; }
    public string? Description { get; set; }
    public string? Status { get; set; }
    public int? ReportOrder { get; set; }

    public virtual ICollection<SlotDatum> SlotData { get; } = new List<SlotDatum>();
}

SlotsDatum controller:

public async Task<IActionResult> Index([Bind("Id,CalDate, Handle,Drop, Win, SlotCount, Denom_id, Description, ReportOrder, [Status]")] SlotDatum slotDatum)
{
    var flashBudgetContext = _context.SlotData.Include(s => s.Denom);

    GlobalCalDate globalCalDate = new GlobalCalDate();
    globalCalDate.CalDateOnly = slotDatum.CalDate;
    ViewBag.CalDate = globalCalDate.CalDateFormatted;
    ViewBag.CalDateOnly = globalCalDate.CalDateOnly;

    // This works for SlotsData but does not return any info from SlotsDenom model even though the stored proc. includes this data
    try
    {
        if (slotDatum.CalDate != DateOnly.MinValue)// && (tblStat.CalDate != null))
            return View(await _context.SlotData.FromSqlRaw("EXEC sp_GetSlotData " + "'" + slotDatum.CalDate + "'").ToListAsync());
        else
            return View(await _context.SlotData.FromSqlRaw("EXEC sp_GetSlotsData " + "'" + DateOnly.FromDateTime(DateTime.Now) + "'").ToListAsync());
    }
    catch (Exception ex)
    {
        Problem("Entity set " + ex.Message + " " + "  is null.");
        return View(await _context.SlotData.FromSqlRaw($"sp_GetLastSlotsData").ToListAsync());
    }
}

// GET: SlotDatums/Details/5
// This works great for both SlotsData and SlotsDenom data.
public async Task<IActionResult> Details(int? id)
{
    if (id == null || _context.SlotData == null)
    {
        return NotFound();
    }

    var slotDatum = await _context.SlotData
        .Include(s => s.Denom)
        .FirstOrDefaultAsync(m => m.Id == id);

    if (slotDatum == null)
    {
        return NotFound();
    }

    return View(slotDatum);
}

這是我的 SQL 加入:

SELECT
    Id,
    Cal_Date,
    SUM(Handle) AS 'Handle',
    SUM([Drop]) AS 'Drop', 
    SUM(Win) AS 'Win',
    SlotCount,
    denom.Denom_id,
    denom.[Description] AS 'Description',
    ReportOrder,
    [Status]
FROM
    Slot_Data sd
RIGHT OUTER JOIN
    SlotDenom denom ON denom.Denom_id = sd.Denom_id
WHERE
    denom.ReportOrder IS NOT NULL
    AND Cal_Date = @sBusDate
GROUP BY
    SlotCount, denom.Denom_id, [Description], ReportOrder, 
    [Status], Id, Cal_Date
ORDER BY 
    ReportOrder

這不是 EF 的特性; 您必須具有與 FromSqlRaw 的 output 相匹配的單一類型。 你可以在這里打開一個問題: https://github.com/do.net/efcore/issues

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM