OT: Linq and stored procedures

OT: Linq and stored procedures

Old forum URL: forums.lhotka.net/forums/t/4391.aspx


ajj3085 posted on Friday, February 22, 2008

Hi,

I'm trying to write some code to execute a stored procedure.  My old way of doing it I had a DataCommand subclass which would have properties for the parameters and attributes would help in mapping everything to the right procedure.

What I've found for linq is to do this:
        /// <summary>Updates a document's ship
        /// date ONLY.</summary>
        /// <param name="shipDate">The new value for the
        /// ship date.</param>
        /// <param name="documentId">The id of the document to update.</param>
        /// <returns><c>true</c> if the update succeeded,
        /// false otherwise.</returns>
        [Function( Name = "dbo.apUpdateDocumentShipdate", IsComposable = false )]
        [return: ResultType( typeof( bool ) )]
        public bool UpdateDocumentShipdate(
            [Parameter( DbType = "datetime", Name = "@ShipDate" )]
            DateTime? shipDate,
            [Parameter( DbType = "int", Name = "@DocumentId" )]
            int documentId
        ) {
            return (bool)ExecuteMethodCall(
                this,
                (MethodInfo)MethodInfo.GetCurrentMethod(),
                shipDate,
                documentId
            ).ReturnValue;
        }

Ok, kinda odd.  It'd be nice if the compiler would generate code for the method body, since the attributes tell it everything it needs.  I guess that wouldn't be good for the compiler though.  My reservation is that the help for ExecuteMethodCall says I shouldn't be calling that method myself. 

Everything I've seen has me using VS to generate my classes for the db... but I already have them because my custom data layer needed them, so I'm able so far to just slap the Linq.Mapping attributes on the classes and go.  Should I be ok, since this is what would be generated anyway?

More on a side note.. anyone know how to use the designer in VS to see my existing objects and decorate them.. or generate the diagram after I've decorated them appropriately?

RockfordLhotka replied on Sunday, February 24, 2008

As far as I know there is no problem with you creating/attributing your own code rather than using the designer. The designer is merely a code-generator after all, and they just code-gen using the attributes, etc.

ajj3085 replied on Monday, February 25, 2008

Ok.  Just wasn't sure if it should only be called from within a System.Linq assembly or not.. thanks!

Copyright (c) Marimer LLC