none
stored procedure not working EF 4.0 RRS feed

  • Question

  •  

    I have mapped the entity to the procedure for insert/update/delete.

    I am using EF 4.0 and sql server 2008

    When I click savebutton it is not updating the DB.

    1. do I have to mention before save changes   addobject for insert, applycurrent values for update etc.

    private void saveall_Click(object sender, EventArgs

    e)

    {

    fsecContext =

    new fsecEntities

    ();

    fsecContext.SaveChanges();

    }

    Thursday, December 22, 2011 12:52 PM

Answers

  • Hi yesganesh;

    Although you created a ObjectContext in the mquery_Click event and filled a data grid view with the data from a query that ObjectContext went out of scope and the entities in the data grid are now detached from any ObjectContext. When you created a new ObjectContext in the saveall_Click event this new ObjectContext has no knowledge of the detached entities from the last ObjectContext and therefore has no entities to update.

    Please when posting code for us to look at use the   button in the edit window toolbar to format it so that it is easily readable.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 22, 2011 4:41 PM

All replies

  • Herewith I have included the code, sp and the mapping of sp in EF4. The sp is not executed during savechanges. Where I am wrong .. got stuck ....

     

    private void saveall_Click(object sender, EventArgs

    e)

    {

    fsecContext =

    new fsecEntities

    ();

    fsecContext.SaveChanges();

    }

    stored procedure------------------
    USE [fsec]
    GO
    /****** Object:  StoredProcedure [dbo].[Insertmmodulestudmarks]    Script Date: 12/22/2011 18:06:29 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[Insertmmodulestudmarks]
    @ayear as Int ,
    @fsecno as nVarchar(20),
    @cid as int,
    @moduleid as nVarchar(10),
    @repeat   as int,
    @modulename  as varchar(150),
    @cwmaxmarks as dec(8,2),
    @cwmarksobtained as dec(8,2),
    @exammaxmarks as dec(8,2),
    @exammarksobtained as dec(8,2),
    @totmarksobtained as dec(8,2),
    @percentage  as dec(8,2),
    @remarks     as varchar(150)
    as
    
    Begin
        
    	--Print @Code
    	--Print @Designation
    	Insert Into mmodulestudmarks
    				Values
    	(@ayear,@fsecno,@cid,@moduleid,@repeat,@modulename,@cwmaxmarks ,@cwmarksobtained,@exammaxmarks ,@exammarksobtained ,@totmarksobtained ,@percentage,@Remarks)
    
    
    
    
    End
    ====
    
    USE [fsec]
    GO
    /****** Object:  StoredProcedure [dbo].[updatemmodulestudmarks]    Script Date: 12/22/2011 17:44:59 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[updatemmodulestudmarks]
    @ayear as Int ,
    @fsecno as nVarchar(20),
    @cid as int,
    @moduleid as nVarchar(10),
    @repeat   as int,
    @modulename  as varchar(150),
    @cwmaxmarks as dec(8,2),
    @cwmarksobtained as dec(8,2),
    @exammaxmarks as dec(8,2),
    @exammarksobtained as dec(8,2),
    @totmarksobtained as dec(8,2),
    @percentage  as dec(8,2),
    @remarks     as varchar(150)
    as
    
    Begin
        
    	--Print @Code
    	--Print @Designation
    	update mmodulestudmarks set 				
    				modulename = @modulename,
    				cwmaxmarks = @cwmaxmarks,
    	cwmarksobtained=@cwmarksobtained,
    	exammaxmarks=@exammaxmarks ,
    	exammarksobtained=@exammarksobtained ,
    	totmarksobtained=@totmarksobtained ,
    	percentage=@percentage,
    	remarks=@Remarks
    	where ayear= @ayear and fsecno=@fsecno and cid=@cid and moduleid=@moduleid and repeat=@repeat
    
    
    End

    Thursday, December 22, 2011 2:12 PM
  • Hi yesganesh;

    From the code you posted no updates will happen because the ObjectContext has no object in it that were modified since it was created. Have a look at this short video to see how to do inserts, updates and deletes using stored procedures with EF.

    private void saveall_Click(object sender, EventArgs e)
    {
        // Create the ObjectContext
        fsecContext = new fsecEntities();
       
        // Save the object that were modified from
        // the ObjectContexe to the store, No objects in ObjectContext
        // No updates needed.
        fsecContext.SaveChanges();
    }


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 22, 2011 3:31 PM
  •  

    This is a re-post of the question found here.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/13f1d2db-387a-4295-b408-2a65b7735958

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 22, 2011 3:35 PM
  • Thanks for the reply.

    I have a dg and the mmodulestudmarks data is populated in the dg using the following.

    the sp also mapped to the entity mmodulestudmarks.

    private

     

    void mquery_Click(object sender, EventArgs

    e)

    {

    iayear = (

    int

    )studlistGridView.CurrentRow.Cells[0].Value;

    ifsecno = (

    string

    )studlistGridView.CurrentRow.Cells[2].Value;

    icid = (

    int

    )studlistGridView.CurrentRow.Cells[4].Value;

    fsecContext =

    new fsecEntities

    ();

     

    ObjectParameter mmyear = new ObjectParameter("ayear"

    ,@iayear);

     

    ObjectParameter mmfsecno = new ObjectParameter("afsecno"

    ,@ifsecno);

     

    ObjectParameter mayear = new ObjectParameter("acid"

    ,@icid);

    fsecContext.Insertintommodulestudmarks(@iayear, @ifsecno, @icid);

     

    var studmarksquery = from m in

    fsecContext.mmodulestudmarks

     

    where

    m.ayear == iyear && m.cid == icourseid && m.fsecno == ifsecno

     

    orderby

    m.moduleid

     

    select

    m;

     

     

    studmarkGridView .DataSource = studmarksquery ;

     

    // this.studmarkGridView.DataSource = ((ObjectQuery)studmarksquery).Execute(MergeOption.AppendOnly);

     

     

     

     

    }

    Thursday, December 22, 2011 4:19 PM
  • Hi yesganesh;

    Although you created a ObjectContext in the mquery_Click event and filled a data grid view with the data from a query that ObjectContext went out of scope and the entities in the data grid are now detached from any ObjectContext. When you created a new ObjectContext in the saveall_Click event this new ObjectContext has no knowledge of the detached entities from the last ObjectContext and therefore has no entities to update.

    Please when posting code for us to look at use the   button in the edit window toolbar to format it so that it is easily readable.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 22, 2011 4:41 PM
  • Hi yesganesh,

    Welcome to MSDN Forum.

    Please upload your file to a free space and paste the link here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 7:46 AM
    Moderator