I have created a Stored Procedure that generates a GUID and then returns it as a NVARCHAR, however when trying to access it using LINQ to SQL it is not being recognized as an ISingleResult. I have created numerous other procedures that have worked as expected such as:
CREATE PROCEDURE [dbo].[GetContact]
@ContactID [int]
AS
BEGIN
SET NOCOUNT ON;
SELECT
ContactID,
PrefixTypeID,
FirstName,
MiddleName,
LastName,
Suffix,
LanguageTypeID,
Organization,
OrganizationLocation,
OrganizationDepartmentTypeID,
OrganizationEmployeeID,
JobTitleID,
TimeZone,
AccountID,
PortalID,
CorporateAccountID,
ContactRelationshipTypeID,
EthnicityID,
CitizenshipID,
EducationID,
ContactSalaryRangeID,
Age,
Birthday,
IsMarried,
LeadSourceTypeID
FROM Contact
WHERE ContactID = @ContactID
END
using (Data.Contact.ContactDataContext contactDC = new Data.Contact.ContactDataContext())
{
Data.Contact.GetContactResult contactResult = contactDC.GetContact(this.ID).SingleOrDefault();
}
However when I try to accomplish the same thing using the following Procedure I get the following error Cannot implicitly convert type 'System.Data.Linq.ISingleResult <OTS.Data.Application.GetSessionResult>' to 'OTS.Data.Application.GetSessionResult'. An explicit conversion exists (are you missing a cast?)
CREATE PROCECURE [dbo].[GetSession]
AS
DECLARE @SessionID [nvarchar](38) = NEWID()
INSERT INTO SessionTable (SessionID) VALUES (@SessionID)
SELECT @SessionID As SessionID
When I run this in SSMS it returns the correct data, however in LINQ it does not. The other questions I’ve found similar to this one have either been unanswered or the answer was to not use LINQ. Is there a way to accomplish this using LINQ?
UPDATE
The code for GetSession is exactly the same as the GetContact. I have tried casting it but receive the error listed above.
The issue I’m experiencing is similar to this: Question about linq2sql
However we have tried removing and re-creating the dbml with no success…not only that but all new procedures that are created are now producing the same issue.
Using a UDF gives the output I need, however it does not work for my application. In my procedures I am checking for the existence of records before returning the data.
example
CREATE PROCEDURE [dbo].[GetPage]
@PageName [nvarchar](255),
@PagePath [nvarchar](255)
AS
BEGIN
SET NOCOUNTON;
DECLARE @PageID [int]
IF NOT EXISTS(SELECT PageID FROM Page WHERE PagePath = @PagePath)
BEGIN
INSERT INTO Page(PageName, PagePath)
VALUES(@PageName, @PagePath)
SET @PageID = SCOPE_IDENTITY()
END
ELSE
BEGIN
SELECT @PageID = PageID
FROM Page
WHERE PagePath = @PagePath
END
--For the return values I have tried both ways below with the same results
--ReQuery DB for data
SELECT PageID, PageName, PagePath
FROM Page
WHERE PageID = @PageID
--Query the Variables
SELECT @PageID AS PageID, @PageName AS PageName, @PagePath AS PagePath
END
With the inability to do an INSERT or an EXEC inside of a UDF I cannot use them and need to use a SP. Any ideas are appreciated.
Oversight
Make sure that you do not forget to include
using System.Data.Linqas well as your data context.