Adding Native View To Model |
![]() ![]() |
Adding Native View To Model |
Nov 3 2009, 06:58 PM
Post
#1
|
|
|
|
Hello,
When I run the SQL on page 368 in the section 'Adding Native View to Model' in SqlServer, the correct records are returned. I'm using ContactID 199. Two payments: $800.00 and $1500. I created the Function: CODE <Function Name="PaymentsforContact" IsComposable="false" > <CommandText> SELECT Payments.PaymentDate, Payments.Amount, Reservations.ReservationDate, Contact.FirstName, Contact.LastName, Events.StartDate, Events.EndDate, Locations.LocationName FROM Payments INNER JOIN Reservations ON Payments.ReservationID = Reservations.ReservationID INNER JOIN Contact ON Reservations.ContactID = Contact.ContactID INNER JOIN Events ON Reservations.EventID = Events.EventID INNER JOIN Locations ON Events.LocationID = Locations.LocationID WHERE Contact.ContactID = @ContactID </CommandText> <Parameter Name="ContactID" Type="int" Mode="In" /> </Function> When I execute the following line ofo code: CODE var payments = context.PaymentsforContact(199).ToList(); the List contains 2 objects of type 'vPaymentsforPeriod' as expected. Oddly, they both contain the same data, the payment made on 4/6/2008 for $800. I was expecting the second object to contain data for the other payment made on 1/1/2008 for $1500. Any thoughts?? Thanks, Bob |
|
|
|
Nov 3 2009, 10:58 PM
Post
#2
|
|
|
|
I guess the first thing I'm required to ask is: did you verify the TSQL directly? I.e. run the query in Visual studio or SQL Server Management Studio?
|
|
|
|
Nov 4 2009, 02:25 AM
Post
#3
|
|
|
|
....did you verify the TSQL directly? Yup, mentioned that in the first clause of my post. Runs fine in SQL Server Management Studio. Returns two correct records. Its the identical SQL used in the book. Just returns two identical 'vPaymentsforPeriod' objects when run in code. Running: CODE var payments = context.PaymentsforContact(199).ToList(); foreach (var payment in payments) { Console.WriteLine("{0} {1}", payment.PaymentDate.ToString(), payment.Amount.ToString()); } outputs: 4/6/2008 12:00:00 AM 800.0000 4/6/2008 12:00:00 AM 800.0000 I'd was hoping it would output: 4/6/2008 12:00:00 AM 800.0000 1/1/2008 12:00:00 AM 1500.0000 Kinda' odd. Thanks for the reply. Bob |
|
|
|
Nov 4 2009, 03:10 AM
Post
#4
|
|
|
|
Sorry, I am travelling and thought the function you created was different than the sql on page 368 since I don't have a copy of my book. therefore I am did not realize that it was the same SQL in both cases.
I am at a conference right now getting ready for some talks and won't be able to investigate right away. I'd want to go back to my original examples that I built which I can't do at the moment. I have no idea why there would be a difference between what the function returns and the what SQL Server returns. Possibly a difference in relationships in the model or something. |
|
|
|
Nov 4 2009, 12:48 PM
Post
#5
|
|
|
|
[errant post, my bad]
This post has been edited by Bob Bedell: Nov 4 2009, 12:55 PM |
|
|
|
Nov 4 2009, 12:54 PM
Post
#6
|
|
|
|
No problem. Enjoy the conference if you can. Public speaking makes me want to chew on table legs.
I'm off to the next topic (glad to have your book by the way), and will revisit Native Queries again the next time they cross my path. There are much more interesting things in the EF to get confused about. Best, Bob |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 22nd November 2009 - 04:41 PM |