none
Alterning EF CORE SaveChanges() RRS feed

  • Question


  • I am trying to learn how to alter the Entity Frameworks normal savechanges() behavior. In short writing and saving to data is done through stored procedures. To accommodate retrieving data, I modified my dbContext to hold a method to retrieve data. My class looks as follows:

     public partial class AccountClientContext : DbContext
        {
            public virtual DbSet<Account> Account { get; set; }
    
            
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {            
    
                modelBuilder.Entity<Account>(entity =>
                {                
                    entity.Property(e => e.Id)
                        .HasColumnName("id")
                        .ValueGeneratedNever();
    
                                    entity.Property(e => e.OpenDate).HasColumnType("datetime");
    
                    entity.Property(e => e.Status).HasColumnType("varchar(500)");
    
                    entity.Property(e => e.Type).HasColumnType("varchar(500)");
    
                    entity.Property(e => e.UpdtDate).HasColumnType("datetime");
    
    
                });           
                    
            }
    
    
            public Task<List<Account>> getAccountData()
            {
                return (Account.FromSql("usp_getAccount").ToListAsync());
            }
            
        }
    
    

     I have read that in my modelBuilder I should be able to map my SP's. However, when I try the following code,

     modelBuilder.Entity<Account>.MapToStoredProcedure( ......)


    I am told that I missing a reference. I do include the following namespaces

    using System;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    Can someone point me to what reference I should be using?

    Is there a better way to alter the EF add(), update() , delete() and savechages() with using SP's?

    Thanks for your help.

    Dann055

    Monday, July 17, 2017 7:42 PM

All replies

  • Hi dann055,

    >>Can someone point me to what reference I should be using?

    As far as I know, entity framework core does not support stored procedure mapping for CRUD operations.

    >>Is there a better way to alter the EF add(), update() , delete() and savechages() with using SP's?

    The following link provide a workaround.

    For Query, which using FromSql melthod.

    For INSERT/UPDATE/DELETE, which using ExecuteSqlCommand, such as

    #stored procedure

    CREATE PROCEDURE usp_InsertCategory
     @CategoryName Varchar(300)
    AS
    BEGIN
        SET NOCOUNT ON;
        Insert into Categories Values (@CategoryName)
    END
    GO

    #usage

    var catName = "Personal Care";
    dataContext.Database
               .ExecuteSqlCommand("usp_InsertCategory @p0", catName);

    http://www.talkingdotnet.com/how-to-execute-stored-procedure-in-entity-framework-core/

    Note:This response contains a reference to a third party World Wide Web site.Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, August 1, 2017 9:05 AM
    Moderator