17751

LINQ2SQL - Stored procedure doesn't generate class in designer

Question:

I'm basically trying to add a set of stored procedures to my LINQ2SQL via the designer.

It adds the stored procedures to the method panel but doesn't generate a class on the designer (although it does in the designer code behind.

It gives the return types a bizarre name based on the stored procedure and basically I'm trying to work out the best way of changing the return type to something more meaningful.

I know I can manually create classes on the designer but I have 5 very large classes and wondered is there a way of creating these automatically or just renaming the return types correctly?

I found this question but I think this involves writing your class by hand and then doing a bit of a hack so wasn't sure about it:

<a href="https://stackoverflow.com/questions/927006/linq-to-sql-cant-modify-return-type-of-stored-procedure/1345208#1345208" rel="nofollow">LINQ to SQL -- Can't modify return type of stored procedure</a>

Answer1:

See <a href="http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx" rel="nofollow">this post</a> by Scott Guthrie.

If you scroll down to the section on "Mapping the Return Type of SPROC Methods to Data Model Classes", you can see that if you drag the Stored Procedure onto a particular model class in your LINQ2SQL designer, it will tell the designer that you want the stored procedure to return a collection of that model type.

You can also look into the ways people handle returning multiple types of data from stored procedure as a method of dealing with your problem as well.

Answer2:

I'm not sure about adding the class to the table, but here is one way of 'renaming' the return type of the sproc.

The following steps assume a stored procedure named "GetSomeThings" and a dataContext called "DataContext".

When you add the sproc to the designer, rename it to "GetSomeThingsPrivate". in the designer.cs file there will now be defined a class called "GetSomeThingsPrivateResult".

Create a new class called "Thing". define a new partial class as follows: public partial class GetSomeThingsPrivateResult : Thing {}

define a new partial class as follows:

public partial class DataContext { public IEnumerable<Thing> GetSomeThings() { return GetSomeThingsPrivate.Cast<Thing>(); } }

now you can call the GetSomeThings() method, which will call the "GetSomeThings" stored procedure, and what you get back will be an IEnumerable of Thing, instead GetSomeThingsResult.

Note that the designer generated method returns ISingleResult whereas the new method returns IEnumerable. If you need ISingleResult functionality then you will need to find an additional workaround - I didn't spend much time trying to get that working.

Recommend

  • Regex - Match Last Occurance
  • Why the time complexity of an array insertion is O(n) and not O(n+1)? [duplicate]
  • Shift operation implementation in java
  • Overflow: auto on html,body breaks jQuery .animate scroll to top?
  • PXAction seemingly does nothing
  • Disable div click on Ajax start and re-enable it on Ajax complete
  • How to count amount of elements in a row of a matrix in C
  • Many to Many in Linq using Dapper
  • Multiple Left Join LINQ-to-entities
  • Command line installation of Code Signing certificates, .p12 files, and mobileprovisions
  • Failed to find version-tag string. File must be updated
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Exception creating JSON with LINQ
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Jquery Knockout: ko.computed() vs classic function?
  • CSS bleed-through with cfinput type=“datefield”
  • Azure table store snapshot/backup capability
  • Linq Merge lists
  • Android changing fragment order inside FragmentPagerAdapter
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How to get current document uri in XSLT?
  • SharedPreferences or SQLite Database?
  • How to define and use opencv mat of user type
  • Allowing both email and username for authentication
  • Ajax Loaded meta Tags
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Get one-time binding to work for ng-if
  • Xamarin Forms - UWP Fonts
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Azure Cloud Service Web Role web pages do not load
  • Fetching methods from BroadcastReceiver to update UI
  • swift auto completion not working in Xcode6-Beta
  • Bug in WPF DataGrid
  • Excel - Autoshape get it's name from cell (value)
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How do you troubleshoot character encoding problems?
  • Arrays break string types in Julia
  • WPF Applying a trigger on binding failure
  • Django query for large number of relationships