Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Playing with DLINQ and Stored Procedures

Blogs

Mike Taulty's Blog

Elsewhere

I've been playing a bit with DLINQ generally and with stored procedure execution a little bit.

If I'm using something like Northwind and its customer table then I create a stored procedure such as;

create procedure dbo.GetCustomersByCountry
@country nvarchar(30)
as
set nocount on

select CustomerID, CompanyName, ContactName, ContactTitle, Address,City, Region, PostalCode, Country, Phone, Fax, recordVersion
from dbo.Customers
where Country=@country
go

And that's all fine. And if I use the sqlmetal.exe tool with its /sprocs flag then I can go ahead and get a method to use to invoke that stored proc as in;

  [StoredProcedure(Name="GetCustomersByCountry")]
  public StoredProcedureResult<GetCustomersByCountryResult> GetCustomersByCountry([Parameter(DBType="NVarChar(30)")] string country) {
    return this.ExecuteStoredProcedure<GetCustomersByCountryResult>(((MethodInfo)(MethodInfo.GetCurrentMethod())), country);
  }

The "problem" I have with this is that it returns some kind of enumeration of GetCustomersByCountryResult but really what I want it to return is IEnumerable<Customers> because otherwise in my program I have two types (Customer,GetCustomersByCountryResult> which look identical but aren't actually related (clearly, I could project one into the other).

My other "question" with GetCustomersByCountryResult is that it has no association with the table called "Customers" in the database and, so, any changes that I make through this type will not show up back in my database when I call SubmitChanges.

So, I ended up hacking it around a bit. I changed the definition that SqlMetal produced for me into;

[StoredProcedure(Name="GetCustomersByCountry")]
public StoredProcedureResult<Customer> GetCustomersByCountry([Parameter(DBType="NVarChar(30)")] string country) {
return this.ExecuteStoredProcedure<Customer>(((MethodInfo)(MethodInfo.GetCurrentMethod())), country);

and that gives me back an enumeration of Customer just fine and I altered my calling code so that it did something like;

IEnumerable<Customer> customers = 
      from c in context.GetCustomersByCountry("UK")
      select c;

    foreach (Customer res in customers)
    {
      context.Customers.Attach(res);
      res.Country = "GB";
    }
    context.SubmitChanges();

 

and that seems to give me the effect that I want but I'm not at all sure that it's the right thing to be doing and, if it is, I feel like the generation layer should give me an option to get it all done there.

(I'm still on the May CTP so lots of things might have changed here).


Posted Sun, Feb 4 2007 8:56 AM by mtaulty

Comments

Marc My Words wrote Mike is looking at LINQ
on Wed, Feb 7 2007 4:33 AM
Mike Taulty's blog - he's a colleague of mine here in Reading - has a load of great content. Very recently
Mike Taulty's Blog wrote Stored Procedures in the March CTP
on Sat, Mar 3 2007 5:05 AM
A couple more &quot;tit-bits&quot; that dropped out whilst playing with the March CTP. It seems that unlike in...