LINQ2SQL - Stored procedure doesn't generate class in designer
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:
LINQ to SQL -- Can't modify return type of stored procedure
See this post 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.
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.
精彩评论