Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to define the data model as a SQL variable not a table or view in EF Core?

$
0
0

I can't figure out if I properly defined the data model as a SQL variable, not a table or view, in EF Core, because the SQL Server's stored procedure and the Blazor web app are successfully communicating with each other.


I am using SQL Server 2017 and .NET 9, here's my data model:

[Keyless]public class SqlOutput{  [Column("SP_OUTPUT")]  public string? SpOutput { get; set; }}

And here's how I set the SqlOutput data model to the dbContext:

public DbSet<Models.SQLServer.SqlOutput> SqlOutput { get; set; }protected override void OnModelCreating(ModelBuilder modelBuilder){    modelBuilder.Entity<Models.SQLServer.SqlOutput>()        .HasNoKey()        .ToView(null);}

I was expecting that EF Core would treat the given data model as a variable entry from the stored procedure, and not as a table object or view.

Here's the relevant stored procedure:

CREATE PROCEDURE myStoredProcedure    @id INT,    @random_text VARCHAR(50),    @key VARCHAR(100)ASBEGIN    SET NOCOUNT ON;    DECLARE @successful VARCHAR(20) = 'SUCCESSFUL';    IF @key = 'MOD_REC'    BEGIN        UPDATE myDB.dbo.myTable        SET random_text = @random_text        WHERE id = @id AND disable = 0        IF @@ROWCOUNT > 0        BEGIN            SELECT @successful AS SP_OUTPUT        END        ELSE        BEGIN            SELECT NULL AS SP_OUTPUT        END    ENDEND

In this C# code snippet:

var result = await _context.SqlOutput    .FromSql(sqlParam)    .ToListAsync();return result.FirstOrDefault();

The expected UPDATE statement script is executed properly, except for the response, which is either SUCCESSFUL or NULL. Instead, it gives this result from the VARIABLES section during debugging using a breakpoint:

debug log details

Do I define the data model improperly, or am I missing something here?


Any help would be greatly appreciated!


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>