Saturday, November 29, 2008

Along with Stored Procedures, you can bring UDFs into an EF Entity Data Model which also surfaces as a function in SSDL.

Unlike Stored Procedures, however, a UDF is composable on the server side and this is reflected in its attributes.

  <Function Name="ufnLBtoKG" ReturnType="nvarchar" Aggregate="false" BuiltIn="false" NiladicFunction="false"
IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"> <Parameter Name="Pounds" Type="int" Mode="In" /> </Function>

But the differences don't end there.

Implementing a UDF and querying with a UDF is not documented anywhere that I can find except that I finally found a hint buried in an MSDN forum thread.

The compiler also gave me a few hints when I was doing things incorrectly.

1) UDFs do not get mapped back to the CSDL via function mapping. This is because of their composability. You need to call them directly from SSDL which is unlike any other querying in Entity Framework.

2) Like many of the mapped functions that come from stored procedures, you can only call these from Entity SQL.

Mapped functions that return an entity can be called from the objectcontext. Everything else must be called via Entity SQL.

Here is an Entity SQL expression that uses the ufnLBtoKG function.

SELECT c.LastName, c.WeightInPounds, MyModel.Store.ufnLBtoKG(c.WeightInPounds) FROM MyEntities.Contacts AS c

The function is called using its strongly typed name but from the store layer, not the conceptual layer.

Since it took me a while to figure this out,  I thought I would save someone that aggravation.

If you follow what feels like the normal steps and map the ufnLBtoKG function back to the CSDL, when EF attempts to execute a query that uses it you get this exception message:

A FunctionImport is mapped to a storage function 'MyModel.Store.ufnLBtoKG' that can be composed.
Only stored procedure functions may be mapped.

That's pretty clear although it confused me at first because I didn't understand how to call the function yet but I finally got it worked out.

Saturday, November 29, 2008 11:57:07 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Thursday, November 27, 2008

In EDM misconceptions, Alex James wrote about something he and I discussed at DevConnections - a diagram in one of my slides that says "EDM" where he thinks it should be "EDMX".

But I beg to differ (respectfully, of course :-)) about the representation in the diagram.

Here is the offending diagram that caused Alex such pain.

objsvcs

On the right, I show the CSDL, MSL and SSDL files that make up the EDM as seen by the Entity Framework.The EF's EDM has a conceptual model as well a means to interact with a backing store, something other EDMs may or may not have.

But to me that is still "an EDM", not EDMX. EDMX is the XML file in Visual Studio that contains the three sets of XML that go into the CSDL, MSL & SSDL files at runtime along with the metadata about where the entities get positioned on the EDM Designer's surface.

But at runtime, the application is not using that EDMX file. It's using "Entity Framework's implementation of an Entity Data Model" which is the three separate files:  *.CSDL,  *.MSL and  *.SSDL. I think referring to them as an Entity Data Model is completely appropriate because I know what that means.

I've spent quite a lot of time thinking and worrying about this as I'm working through final edits of my book and not wanting to commit the "crime" of misrepresenting the information or confusing people further.

It's certainly important to understand that EDM is much bigger than Entity Framework. It is a concept about data modeling and as more of Microsoft's applications embrace the EDM , you will see different implementations of it. While a number of applications will use the same EDM I can build with the ADO.NET Entity Data Model Design tools in Visual Studio, the one that is comprised of three parts and works its way to a backing store, many will use their own implementations and might have their own schemas. We'll have to see how that plays out.

Thursday, November 27, 2008 11:35:05 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Wednesday, November 26, 2008

Srikanth Mandadi of the ADO.NET Entity Framework Team  has begun a series on dealing with big EDMs in Entity Framework v1 on the ADO.NET Team blog. Working With Large Models In Entity Framework – Part 1

This is an important problem that needs some good guidance for solving and I'm very happy to see this series.

I talked with one guy in my workshop at DevConnections earlier this month who has to deal with a model that is for a hospital database and would have hundreds and hundred of entities that he can't envision how to break up into smaller models where there are no relationships going across.

One of the options for doing this is with the Using element which allows you to break a model up into multiple CSDL's and still benefit from associations across the two models. It's not a Designer friendly solution and the MSDN docs don't really have much detail on it so I thought I would try to implement it.

When I tried it out a few days ago, I wrote the following which I realize I never posted. And now Srikanth has written about this scenario in more detail in today's post: Working With Large Models In Entity Framework – Part 2. But I still thought I would share my thoughts as well. The bottom line is that as powerful as the Using construct is, it's a pain in the butt to implement.

Starting with AdventureWorksLT, I wanted to strip out the entities for the Product details (ProductModel, ProductDescription, etc)

csdlusinga

I tried creating a second schema right in the EDMX file and adding the Using element to the main schema.

<edmx:ConceptualModels>
     <Schema Namespace="AdventureWorksSuperLTModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<!--Using element pointing to new schema -->

       <Using Namespace="AWProductDetailModel" Alias="BaseModel"/>

     ...entities & associations for main csdl...

    </Schema>

<!--NEW Schema-->

<Schema Namespace="AWProductDetailModel" Alias="Self">
  <EntityType Name="ProductModel">
    <Key>
      <PropertyRef Name="ProductModelID" />
    </Key>
    <Property Name="ProductModelID" Type="Int32" Nullable="false" />
    <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
    <Property Name="CatalogDescription" Type="String" MaxLength="Max" Unicode="true" FixedLength="false" />
    <Property Name="rowguid" Type="Guid" Nullable="false" />
    <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />
    <NavigationProperty Name="Product" Relationship="AdventureWorksSuperLTModel.FK_Product_ProductModel_ProductModelID" FromRole="ProductModel" ToRole="Product" />
    <NavigationProperty Name="ProductModelProductDescription" Relationship="AdventureWorksSuperLTModel.FK_ProductModelProductDescription_ProductModel_ProductModelID" FromRole="ProductModel" ToRole="ProductModelProductDescription" />
  </EntityType>
</Schema>

</edmx:ConceptualModels>

That is unfortunately not how to do it. This is attempting to solve the problem in the EDMX file which is the designer file but the solution is not supported in the designer. You need to do this in raw CSDL files, not the EDMX.

Also, since Using isn't supported by the designer, you wouldn't be able to open the EDMX file in the designer anyway. Additionally you can't use the code gen tool from the designer.

So the way to do this is either start with your entire model in the designer and set the Metadata Artifact property so that it spits out the three files (CSDL, MSL, SSDL) rather than embedding them into the assembly. Then create the separate CSDL, moving the entities that you want separated into there. In the primary CSDL you need not only the Using element, but you'll need to property reference the new Model's name in any reference to the entities that live there (eg in Associations). Then you need to use the manual EDMGEN command line tool to generate your classes.

It's not for the feint of heart or those who, like me, are short on time.

Srikanth included sample xml files in his post so you can see how the puzzle fits together, but I'm curious how the classes surface. I am guessing you still use everything in a single context. Therefore the benefit is less cumbersome models, but since I can't use them in the designer, today that's more cumbersome for me.

I'll be keeping an eye for the rest of the posts in this series which suggest solutions that are supported by the designer.

Wednesday, November 26, 2008 7:51:47 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Tuesday, November 25, 2008

roughcuts_3 I just noticed that 6 more chapters are available on the Rough Cuts site for my book, Programming Entity Framework.

  1. Advanced Entity Data Models
  2. Implementing Stored Procedures: Beyond the Basics
  3. Entities in Web and WCF Services
  4. Relationships and Associations
  5. Making it Real: Connections, Transactions, Performance and more
  6. Take Control of Objects with ObjectStateManager and Metadata Workspace

The Web & WCF Services chapter is the first of two. A later chapter explores a more advanced pattern for WCF that leverages lessons learned in the 2nd half of the book.

Tuesday, November 25, 2008 12:50:42 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

I read an article about Google Apps on CNNMoney.com that caught my eye and couldn't help noticing the inadvertent joke made by the author. However, the joke loses its humor when you realize that it's more about life in the cloud and not solely about Google Apps. Read more here.

[A New DevLife Post]

Tuesday, November 25, 2008 9:00:08 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Monday, November 24, 2008

A reader of the Rough Cuts edition of my book, with only 9 chapters out of 23 available so far, was not able to find the database scripts for the book's sample databases and because of this, to my great dismay, placed a 2-star review of the book on the O'Reilly site. Boy does that sting! The good news was that other than this problem of being unable to locate the database files, he seems to have liked what he's read so far and that makes me happy.

But the databases are available. They are on the the book's web site, not the O'Reilly site. That bit of information is in the book's Preface which is not yet published to the Rough Cuts.  I pointed this out in the feedback thread earlier this month, but perhaps that went unnoticed.

Anyway, if you are looking for them, they are on the Downloads page of the www.LearnEntityFramework.com site.

There are two databases that the book uses for its samples and you can download scripts to build either a SQL Server 2005 version or SQL Server 2008 version for each one.

Monday, November 24, 2008 7:42:30 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [2]  | 
 Sunday, November 23, 2008

Last night Rich and I watched Sherlock Holmes, The Red Headed League on Vermont Public Television. It was heavenly. Why is this significant? Since we moved to Huntington over five years ago, we haven't been able to get Public Television on our rabbit ears. Cable is not an option and I neither of us wanted to go with a satellite dish.

Enter the age of Digital TV. We got our converter box months ago but were disappointed because none of the stations would stick. Every minute or so the digital image would break up and the audio would completely drop out. The plan is to get a newer antenna but we just haven't done it yet. Last night we had the choice between football or a re-run of a recently aired Law & Order. So I said, "Hey, let's try the digital again."

Perhaps it was the weather or just a stroke of good luck but all of the stations were coming in with no problems. What's more, there are four, count 'em F O U R different VPT channels. That's four options of public broadcast quality programs. It was hard to choose between Austin City Limits and Sherlock Holmes, but the clever detective won out.

Who knows if this will last but for now, hallelujah. Oh, how I've missed it all - Antiques Roadshow, the britcoms, Mystery! and more.

Sunday, November 23, 2008 3:36:12 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 

I got myself pretty confused when I was debugging C# and VB code at the same time in a recent DevConnections session. For a good laugh (at my expense...again) click here!

[A new DevLife post]

Sunday, November 23, 2008 10:53:13 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

When I finish writing my book, I'll have my brain back to read other .NET (& related) books. I've got my short list waiting in the wings and you can see it here.

[A new DevLife post]

Sunday, November 23, 2008 10:49:56 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Thursday, November 20, 2008

With my permission, Simon Segal has put together a bunch of tutorials that I have written in my blog as a single PDF. He had done it for himself and asked if it was okay to share. Here's a link to his blog post. Julie Lerman Entity Framework Tutorials consolidated

Perhaps I'll get his PDF and put it on my learnentityframework.com website as well.

Thursday, November 20, 2008 3:43:27 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Wednesday, November 19, 2008

At the end of one of my sessions at DevConnections last week, a girl friend of mine from Burlington who was at the conference came into the room and sat down with a big grin on her face. It seems that she and another girl friend of mine, a programmer from Alaska, had made an executive decision about that evening's entertainment and decided that I would be joining them. She was delivering the ticket they bought for me. Was I finally going to see a Cirque du Soleil show? Maybe Bette Midler (who had been on my flight to Vegas)? I had no clue. I turned a few shades of red when I saw the ticket and with a whole lot of coaxing, did show up at the venue to meet up with the girls at the designated time.

The show was silly, a little weird, but I had to have a serious memento to prove that I had succumbed to attending.

 

 

 

....

 

 

 

wait for it

 

 

 

 

.....

 

 

 

 

las vegas

Yes folks, its true. I went to see the famed Thunder from Down Under show.

That's all your getting out of me. ;-)

Perhaps this will convince more women to attend developer conferences?

Wednesday, November 19, 2008 7:28:54 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [3]  | 

One of my favorite conferences is approaching in a few more weeks - DevTeach, which will be held in (nearby, for me) Montreal from Dec 2-4 with workshops on Dec 1st and 5th.

I have been speaking at DevTeach since its inception in 2004 when it was a once-per-year event held in Montreal. It has become so popular that it is now held twice a year across Canada in Montreal, Toronto or Vancouver. So it's Montreal's turn again which I love for a few reasons. It's only a 3 hour drive from my house and I love Montreal.

The conference recently arranged an unbelievable benefit for attendees. Each attendee will get in their swag bag:

  • Visual Studio 2008 Professional
  • Web Expression
  • A set of DVDs containing all of the content (videos of every session and accompanying powerpoints and code) from TechEd 2008.

In addition, if you have attended one of the recent MSDN Canada TechDay events, there is a $350 rebate on the registration fee.

If you are from the U.S., you can also benefit from the current exchange rate. The Canadian Dollar is about .80 right now.

But wait there's more. I will be teaching a full day workshop on December 5th on, you can probably guess, Entity Framework. The class is only $400 (CN). I was fortunate enough to be given some MSDN Subscriptions earlier this year. I gave two of them to some needy developers and saved the last one to raffle off in my workshop. This is the big fat VSTS2008 Team Suite MSDN Premium Subscription which normally costs $10,000 new and I think about $3000 for a renewal.

These are of course the extras. The real value of DevTeach is the great content, access to some amazing teachers/thinkers in our field, what promises to be a great keynote by Ted Neward and the wonderful networking opportunities offered by this intimate conference.

And the parties of course.

www.devteach.com

Wednesday, November 19, 2008 1:09:02 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 
 Wednesday, November 12, 2008

A few years ago at DevConnections I had a long talk with Gert Drapers (from the SQL Server team) about EF when it was still in an early beta.

I'm now at DevConnections in Las Vegas and caught up with Gert again and had an interesting follow up conversation about EF now that it is released. I really like getting the perspective of folks who bring a different expertise to the table. We talked a lot about stored procs especially the DBA perspective.

The EF designer currently [handily] supports a narrow set of stored procedures for reading and writing. The procedures need to align with entities in the data model.

Read Stored Procs often return all different shapes of data . You can map a read stored procedure to an existing entity if its resultset exactly matches the entity . Then you can call this sproc as a function of the object context, passing in any required parameters.

You can also do function mapping for read stored procs that return a scalar value or randomly shaped data. The designer supports this as well with the Function Mapping wizard, however these functions are not surfaced as methods in your objectcontext class. Instead, you need to call them with EntityClient using ExecuteScalar or ExecuteReader. (The latter was a trick I discovered when experimenting. When doing the mapping, just select maps to "None" in the wizard.)

If the entity doesn't exist, I have been creating a view in the db to represent the resultset and letting the wizard create the entity & entity set & mappings for me. Then I can do the function mapping between the sproc and the entity and call the function as necessary.

Either way however,  as a function,  these don't benefit from any of the querying capabilities of EF. You can query the results  in memory using LINQ to Object, but any additional LINQ methods you apply to the functions will get processed locally and not be part of the store command. Nor do they benefit from the deferred execution that queries give us.

Gert made, what now seems like a totally obvious point and may also be obvious to others . He said

Why not just query against the views? As long as you've got the view, why not just use that instead of the function?

Rather than being tied to the function (stored procedures) and their parameter(s), you can query against the view any way you want providing any additional filters or other operators that you like, which will be part of the store command and be executed on the server. Additionally, you can use LINQ to Entities or ObjectQueries against any of the views, rather than being forced to use EntityClient. The only downside is that you don't get the function arguments to let you know what parameters are normally expected for the procedure.

Wednesday, November 12, 2008 1:47:50 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [3]  | 
 Sunday, November 09, 2008

Free development tools from Microsoft for use in production? What have I been smoking? Check it out...

 

[A new DevLife post]

Sunday, November 09, 2008 9:39:02 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Friday, November 07, 2008

Ever the avoider of controversy, I've kept my head down for the past week so I could finish getting my book into the hands of the copy editor. Of course, now I'm preparing for next week's presentations at DevConnections, including a full day of EF on Monday.

Which is why I haven't really said anything about the brouhaha last week over Tim Mallaileu's posts and those which followed.

I've written about it over on my Z-D blog which, by the way, has been moved again. Now it's part of the blogs.eweek.com site. That's pretty high falootin' company to be keeping so I thought I would write a big fat post about a big fat controversy. ;-)

The future of LINQ to SQL and Entity Framework

]A new DevLife post]

Friday, November 07, 2008 9:10:46 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

While the entire book is now in production, so far Chapters 2-9 have made it into the RoughCuts version.

Here's a full chapter list. THere are still "working titles" on a few of the chapters.

 

1. Introducing ADO.NET Entity Framework

2. EDM Basics

3. Foundations of Querying

4. More Querying (yes, this is a working title!)

5. Basics of Stored Procedures in the EDM

6. A More Practical Model

7. Databinding Entity Framework in Windows Apps

8. Object Services

9. Customizing Objects

10. ASP.NET EntityDataSource Control

11. Advanced Modeling

12. Implementing Stored Procedures: Beyond the Basics

13. Entity Framework in Web & WCF Services

14. Relationships & Associations

15. Makingi it Real: Performance, Transactions and more

16. Take Control of Objects with ObjectStateManager and Metadata Workspace

17. Handling Entity Framework Exceptions (this chapter includes Optimistic COncurrency)

18. Customizing Objects

19. Considerations for using EF in Client Side Application Layers

20. Considerations for implementing Layers in ASP.NET Applications

21. Building a Smarter WCF Service to Work with Entities

22. Entity Framework Today and Tomorrow

Appendix: Assemblies and Namespaces

Friday, November 07, 2008 6:02:02 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [2]  | 
 Thursday, November 06, 2008
Moo

We had some visitors today ... a few ladies who had wandered off from the farm about a 3/4 a mile away. Apparently they had been separated from their calves and went out looking for them.

My office door was closed but the louder than normal mooing finally registered so I looked out my door and what did I see? Two dozen cows headed up the road.

moody road cows 001

Rear view.

moody road cows 003

Bruce (their owner) got them turned around, but then they headed up our driveway.

moody road cows 004

And around in circles.

moody road cows 005

Nothing interesting enough here, so the visit was over quickly.

moody road cows 006

Back down the road.

moody road cows 008

They have to go down our road and then back up their road which is a good 1/2 mile steep climb.

Thursday, November 06, 2008 3:08:53 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [3]  | 
 Wednesday, November 05, 2008

I do have a category in my blog called EEK! Politics; but I rarely use it because there is a lot of political passion in our community which can sometimes be distracting.

But today is certainly a special day.

For those of you who don't happen to know by now that I am frequently referred to in the .NET community as a "tree-hugging hippie from Vermont", there's a reason for that.

67% of Vermont voters chose Obama yesterday. I believe that that is the highest percentage of any state.

And, yes, I was one of them.

And I'm a happy camper today.

Update - oops, I used CNN.com's map to determine this and neglected to hover over states that were detached from the map.

Washington D.C. (no, not a state but...): 93%

Hawaii: 72%

Wednesday, November 05, 2008 11:51:01 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [6]  | 
 Tuesday, November 04, 2008

In the original version of Douglas Adams famed book, "42" was the answer to everything.

Actually it was a typo. What he meant was "V2".

Tuesday, November 04, 2008 1:29:51 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [3]  | 
 Monday, November 03, 2008

The current issue of CoDe Magazine (Nov/Dec 2008) has the usual array of great  technical articles with focus on "Modern User Experience". Look for articles on WPF and on Rocky Lhotkas Silverlight version of CSLA (hooray).

I contributed an MVP Corner essay for the issue telling a story about a project gone awry some 15+ years ago. (Nobody caught the typo that says 25. Oops!)

It's a little funny and, from the perspective of my puppy dog innocence at the time, a bit pathetic. It even has shades of "The Sopranos".

I love that CoDe doesn't edit out terms like "sleazebag". :-)

Check it out: MVP Corner: Good Contracts or Good Friends

Monday, November 03, 2008 9:05:33 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  |