Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

How to directly execute a SQL command with Entity Framework on SQL Server.

17.05.2018 (👁6023)

 

Problem:

Using the Entity Framework in Asp.Net Core, the data is always first fetched into the data model, changed and then saved on the SQL Server via an update.

The EF data model acts as an ADO table cache. Unfortunately, time-sensitive operations can easily cause a data conflict, as the server data is changed in the meantime.

Simple operations, such as incrementing a counter or storing it clean, can therefore be done purely on the SQL server by sending only the SQL-Transact command to the SQL Server.

 

    public static void counter_of_View_erhoehen(long IDUser, long IDNote = 0)

    {

        //-------------< logViewCounter_erhoehen() >-------------

        //< Sum ViewLogs >

        string sSQL = "UPDATE[dbo].[tbl_Notes]  SET[sumViews] = ISNULL([sumViews], 0)+1  WHERE [IDNote]=" + IDNote;

        EF_Model.dbContext.Database.ExecuteSqlCommand(sSQL);

        //-------------</ logViewCounter_erhoehen() >-------------

    }

 

Fast process via EF Execute

EF, Entity Framework,

Asp.Net core 2

 

A simple SQL Server Execute statement below EF in Asp.Net Core

 

 

 

Process via entity framework data model

EF, Ado tables

 

Using secure working with the Entitiy Framework, the data or the individual data set is briefly retrieved,

Then safely processed with all the benefits of C # and SQL Injection safely, and finally uploaded the changed data back to the server with the Update command

EF works internally with the former ADO

Execute a simple SQL Update statement under Entitiy Framework with Database Context

    public static int counter_of_View_erhoehen(long IDUser, long IDNote = 0)

    {

        //-------------< logViewCounter_erhoehen() >-------------

        //< Sum ViewLogs >

        NoteModel note = EF_Model.dbContext.tbl_Notes.SingleOrDefault(m => m.IDNote == IDNote);

        if (note == null) return 0;

 

        if (note.sumViews == null) note.sumViews = 0;

        note.sumViews += 1;

        EF_Model.dbContext.SaveChanges(true);

        //</ Sum ViewLogs >

 

        return Convert_Methods.IntNull_to_Int( note.sumViews);

        //-------------</ logViewCounter_erhoehen() >-------------

    }