Sunday, August 17, 2008

From Microsoft

Entity Framework Supported Mapping Scenarios
Writer: Asad Khan
Published: July 2008
Applies To: Entity Framework
Summary: This white paper enumerates the supported mapping scenarios in Entity
Framework. It also mentions important design considerations for store schema in
order to deal with advanced mapping scenarios.

Sunday, August 17, 2008 10:03:06 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [2]  | 
 Friday, August 15, 2008

Naturally, these are on my mind because my hand still stings from getting slapped by the runtime when I tried to do this.

LINQ to Entities does not support projecting into known types structures.

Note, I tried this first with a structure, then saw the forum thread which I quoted below that I apparently misconstrued and landed on the wrong  conclusion. The post stopped me from experimenting further. I have since edited the post to be improve its accuracy. Thanks to Larry Parker for pointing out the gap.

If you have a type or struct defined such as:

Public Structure KeyListItem
  Public ID As Integer
  Public Name As String
End Structure

and are doing a query that will project out an ID and a Name, this is the natural way to write the query, given some experience with LINQ to [anything (including LINQ to SQL) but Entities].

Dim custs = From cust In context.Customers _
            Select New KeyListItem With {.ID = cust.ContactID, .Name = cust.Name}

The compiler is fine with this because at compile time, it's merely a LINQ query.

However when you execute this query, you will get the error:

Only parameterless constructors and initializers are supported by LINQ to Entities.

LINQ to Entities can't construct a new KeyListItem structure on the fly with the ID and Name parameters. 

Here's an explanation of why this is the behavior for LINQ to Entities from Sushil Chordia on the DP team (from this forum thread).

The idea of supporting only parameter-less constructor was one of the hard decisions we made as a product team. The main idea with this approach was we shouldn't open up new surface area in LINQ over Entities that is not supported by EDM. EDM in general doesn't allow you to construct in random objects (using the NEW constructor in eSQL). To make it consistent with the whole stack, we decided to implement it like wise. Limiting the constructions only to:
      - Parameterless constructors 
      - Anonymous types (as it’s the only way to do multi-project in LINQ over Entities)

The query will work if you project into a class, not a structure. If you must use a structure then, instead you need to go the long way around with this.

Dim custs = From cust In context.Customers  _
               Select cust.ContactID, cust.Name
   Dim custlist = New List(Of KeyListItem)
   For Each cust In custs
     custlist.Add(New KeyListItem With {.ID = cust.ContactID, .Name = cust.FullNameAlpha})
   Next

Okay, now the next thing you might accidentally try if your mind is wandering is to use custom properties from your partial classes in queries.

In my model I have a firstname property and a lastname property. I got sick of concatenating them, so I cleverly created two new properties in the entity's partial class: FullName and FullNameAlpha. A long long time ago, I got over the fact that I couldn't pull this off in the model and I'm extremely happy that this scenario is being targeted for v2.

So I'm happily coding along and writing a query where I want to return the FullNameAlpha field and sorted.

Because LINQ depends on the compiled classes to let us code with Intellisense, it's fine with me using the FullNameAlpha property in my query

Dim custs = From cust In .Customer _
                   Order By cust.FullNameAlpha _
                   Select cust.ContactID, cust.FullNameAlpha

But of course FullNameAlpha is not part of the model, so when EF tries to compile the query, it throws the obvious exception:

The specified type member 'FullNameAlpha' is not supported in LINQ to Entities.
Only initializers, entity members, and entity navigation properties are supported.

Oops. Yeah, right, I knew that!

So what that means is I still have to freaking concatenate the first name and last name in my query.

But there's another rule to watch out for which is a LINQ rule, not LINQ to Entities. When you create an on the fly property in a projection and your are doing so with a function, LINQ cannot figure out what to call that property, so you must name it as I have done here by using the variable name "fullname".

Dim custs = From cust In .Customer _
                   Order By cust.FullNameAlpha _
                   Select cust.ContactID, fullname = cust.LastName.Trim & "," & cust.FirstName

Otherwise you will get the LINQ error:

(VB) Range variable name can be inferred only from a simple or qualified name with no arguments

(C#)  Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

I imagine I'll repeat these errors over and over and have to be reminded by the thrown exceptions over and over.

Friday, August 15, 2008 5:25:31 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [5]  | 

Today I noticed that my name has 3 stars under it on the MSDN forums. :-)

threestars

That's because I've been trying to answer questions while I've been spending a lot of time in there looking to see what types of questions and problems other people have been having with Entity Framework. It's definitely been educational for me and hopefully at the same time, whatever I can feed back into the system is hopefully helpful for others.

If you are learning EF and have questions or problems, please search the Entity Framework forums before you drive yourself batty. It is one of the best concentrated resources available right now besides the documentation. As per my blog post here, you may need to clear out your I.E. Temp Files to get at the updated MSDN Docs.

Friday, August 15, 2008 12:56:59 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Thursday, August 14, 2008

Not everyone is having the easiest time installing this service pack.

Here is a list of hints, links and things you should be prepared for before applying the service pack.

[A New DevLife Post]

Thursday, August 14, 2008 11:26:57 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Wednesday, August 13, 2008

One of the most tedious steps of writing my book is doing the spell checking when I finish a chapter. While there are certainly plenty of honest to goodness typos, there are a lot of things that pop up on the radar - things like field and property names, class, function and method names, words that are upper case for a reason. But mixed in with that are all of my typos and accidental grammar boo-boos. So I have to be very careful not to have a trigger finger on the options of change, ignore once, ignore all and add to dictionary. I'm very happy to be able to add words like OptimisticConcurrencyException to the dictionary and get them out of the way.

Wednesday, August 13, 2008 7:52:43 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 

The Sept/October 2008 issue of CoDe Magazine contains an article that I wrote highlighting some of the major differences between DataSets, LINQ to SQL and Entity Framework. As long as the article is, and it is the longest I have ever written for CoDe at 14 pages, of course it was impossible to cover everything. But I used as my basis my own experience, plus the many questions I have been asked directly or seen on forums and in blogs over the last few years as a basis for what I thought was important to include. I actually overhauled the article one last time just before it went to print based on the feedback I got from presenting on a similar topic at TechEd. The article focuses only on what's "in the box".

The issue, which as always has lots of other great articles - many on programming languages as well as a fun-to-read MVP Corner essay  by the twitter-addicted Chris Williams - is in print but not yet online.

Wednesday, August 13, 2008 7:39:22 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Tuesday, August 12, 2008

I've created a site that will eventually be used for my book, Programming Entity Framework. Before I had a book title I had  started out using LearnEntityFramework.com then pointed ProgrammingEntityFramework.com to the same spot. In the long run the urls just point the thedatafarm.com/learnentityframework. It's all good.

The site uses the free "express" version of Graffiti from Telligent (Community Server's younger sibling) which is easy to use and very customizable.

Right now it is a subset of my book related blog posts along with a feed from my Data Access posts on this blog. It also lists where I'll be doing presentations and workshops.

Downloads for the book will go there as well as errata (if there are any ;-))

We're still waiting for DataDeveloper.net to reappear with all of the tutorials I wrote, the articles, and the compilation of Entity Framework and other data access resources. :-( It's not gone, just moving ... ummm, slowly.

Don't forget the MSDN Data center as well.

Tuesday, August 12, 2008 2:04:15 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 

RESOURCE SYSTEMS GROUP is a multi-disciplinary, employee-owned consulting firm specializing in the planning, analysis, and management of business, infrastructure and natural resources. We serve clients who share our belief that high-quality, objective analysis is a prerequisite to resolving complex problems. More than just analysts, scientists, and engineers, we’re communicators—our study results are clear, concise, and directly applicable to a client’s particular questions and challenges. Our solutions are creative and grounded by 20 years of experience with clients as large as federal government agencies and Fortune 500 companies or as small as neighborhood interest groups and local municipalities. Recognized as one of the “Best Places to Work in Vermont” for the 2nd consecutive year and named as one of the “Best Workplaces for Commuters”, RSG employees enjoy excellent benefits, flexible hours, a commitment to creating a sustainable workplace, and opportunities for advancement. We are an equal-opportunity/affirmative action employer. Please visit www.rsginc.com for more information on Resource Systems Group.

Web and Application Tool Developer

White River Junction, VT

This position supports a group that specializes in complex, web-based survey and panel research employing advanced analytic methods. The ideal candidate is a multi-skilled front and back end developer who is passionate about creating quality user-centered web pages and in-house tools and applications. We need an active team player, a quick learner, and a dynamic innovator. Our work is fast-paced with numerous user-centered, iterative design and development cycles—you can expect to demonstrate progress early and often and receive feedback from every level in the company. The position requires occasional travel. We seek someone with the following qualifications:

  • Passionate and detail-oriented about design, layout, and usability
  • Keen design sense and a strong technical foundation
  • Active engagement with the web development/design industry, including keeping current with evolving and emerging technologies (font-end, back-end, new tools/libraries)
  • Experience or a desire to learn survey research methods and tools
  • Proven ability to learn—and enthusiasm for learning—new programming languages
  • Highly adaptive to change and a promoter of change
  • Good writing, communication, and teaming skills in multiple mediums
  • Excellent problem-solving abilities
  • Bachelor’s degree in Math, CS, CSE preferred

Please send resume and cover letter to Recruiting Director at employment@rsginc.com and indicate Web and Application Tool Developer in the subject heading.

clip_image002 clip_image004 clip_image006

Tuesday, August 12, 2008 1:49:40 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

The starting point for EF in the online documentation is here:

http://msdn.microsoft.com/en-us/library/bb399572.aspx

This will help! You'll need to dump your Internet Explorer Temp files if you are not getting SP1 documentation.

Otherwise you'll see Beta 3 and it will be obvious. Here is a screenshot of the new docs.

efsp1

Tuesday, August 12, 2008 9:39:50 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [8]  | 

Last night's VTdotNET meeting with Nick Petterssen of Electric Rain  talking about the product design and development around StandOut which is written with WPF was really fun. It was very inspiring to see what can be accomplished with WPF in a full product, not just bits and pieces of code samples. There are so many innovations in StandOut also. Not to mention the year that Nick spent researching what makes a presentation great and what  presenter needs to help him or her be great. As a developer this was already interesting. But as someone who presents a lot, I was equally interested in the product and some of the tidbits Nick shared.

Here are links to some things that came up in the various discussions.

The DNRTV episode where Billy Hollis demos a Line of Business application using WPF. We had a long talk about WPF and business applications vs. the types of WPF applications we normally see. What Billy's team is doing with WPF really does much more than make the data entry application shiny.

VBMigration Partner from Francesco Balena's company , CodeArchitects. This was not related to the presentation, but another conversation.

Congrats to the winners of the Infragistics and Resharper licenses (thanks to Infra & JetBrains for the monthly donations) and lots of great books and even some t-shirts. :-)

Next month's meeting is Sept 8th with Mark Merchant from Microsoft on Programming with Virtual Earth.

Tuesday, August 12, 2008 9:27:02 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

I spend a lot of time in Reflector learning about Entity Framework but here's a very oddly named (yet descriptive, don't you think?) method I missed.

In EF, when your entities are being managed by the objectContext, relationships are defined by separate objects. In order to build a graph, the context looks at your entity, then finds all of the RelationshipEntries that contain that entity and from there traverses to the entities on the other end of the relationship object.

Outside of the objectContext, graphs are built the normal CLR way with an entity or collection being a property of another entity.

In a forum thread someone was trying to write generic code without an objectContext to strip an entity of any related entities that might be attached to it.

I had assumed that impacting relationships with methods of RelatedEnd or IRelatedEnd was not an option because of their dependence on the RelationshipManager which requires the ObjectContext.

But (horrors!) I was wrong. I might get over it. Graham Hay just went ahead and solved the problem with IRelatedEnd anyway and in a follow up, Gergely Bachraty pointed out that this was possible because of the internal method GetAllRelationTypesExpensiveWay, which I then saw depends on GetRelationTypeExpensiveWay.

These abilities are important for writing generic code where you can't just say "Order.Details" because you won't know the type of the parent entity or the type of the collection. Not knowing the type of the entity is easy to deal with, but referencing the EntityCollection is a big challenge. Without ObjectStateManager (only available thorough the ObjectContext) or the MetadataWorkspace (easily available thorough ObjectContext, but also can be created with no more than a pointer to the model files), what Brian was attempting to do in this post would likely have been impossible.

While it's not always easy, the flexibility that is available by digging down into the APIs, and the creative solutions that people like Graham, who are digging down in there are coming up with, continue to impress me.

Tuesday, August 12, 2008 8:31:07 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

On the ADO.NET Team blog:

Third Party Provider Support for the Entity Framework RTM

I found this video a few weeks ago from Sybase on YouTube of all places where they talk about SQL Anywhere 11's support for EF when someone had emailed me asking about SQL Anywhere support.

SQL Anywhere 11 supports the Entity Framework

Tuesday, August 12, 2008 6:01:19 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

On the ADO.NET Team blog there's a list of changes between SP1Beta & RTM: What's new in the VS 2008 SP1?

The designer got over 200 bug fixes including a bunch that help with source control and a few of the annoying validation error messages that are incorrect (and send you chasing down a problem that doesn't exist) have been fixed.

There are a bunch of fixes to the EntityDataSource. What I don't see on there is that you can now combine the Include (was IncludePath) and InheritedTypes. I complained about this when I first got my hands on the EDS so was happy to see those two working together. Events have changed a lot. You can see a list of breaking changes in the VS2008 readme file. Or just let Visual Studio point them all out to you, like I did.

There are also some changes in the basic Object Services model with improvements for folks doing databinding because the navigation properties are now available. Happy to see that IsLoaded is fixed so that it correctly reports its status. I've had to overlook the property because I didn't think I could count on it.

Tuesday, August 12, 2008 5:56:58 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Monday, August 11, 2008

The release of Visual Studio Service Pack 1 today (currently available for MSDN Subscribers to download), marks the RTM of Entity Framework and ADO.NET Data Services. It's been a long haul for EF, initially shown off in early 2006. ADO.NET Data Services, or ANDS as I've seen Shawn Wildermuth call it (though I still call it Astoria) is a bit younger, making its first public appearance in March 2007 at MIX.

So they are finally here. There were a lot of doubting Thomas' in the first year or so of EF, thinking it would go the way of some other projects that never saw the light of day.

Congratulations to everyone in the Data Programmability team for taking this vision and making it real. And this is just the start. The vision for Entity Data Model is very big and there is much more to come for clients of Entity Data Models. And this first release of Entity Framework for .NET developers to use an EDM is just the beginning as well, with  V2 is already in the works.

I also think that today is a good day to give a nod to a man who helped EF get on it's first legs, and you'll find his name as an author of this early whitepaper about EF, Next-Generation Data Access: Making the Conceptual Level Real - Jim Gray.

Monday, August 11, 2008 3:58:11 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

Electric Rain is one of the darlings of the MIX conferences thanks to their early adoption of WPF in creating the very cool StandOut application.

Although the company is based on Colorado, Product Design Manager, Nick Petterssen lives nearby, just outside of Montpelier, VT.

Nick is presenting at tonight's VTdotNET meeting and we're really looking forward to it.

Topic: Designing .NET Apps with Style – A StandOut Case Study
Join Nicholas Petterssen, Director of Product Design for Electric Rain, for a look into how design can differentiate your .NET apps with the new developer/designer workflow Microsoft is touting in .NET 3.0 and beyond. Learn what WPF, Expression Blend and a good interactive designer can do for your projects in the “Sexy App” department and experience how the rich media, animation and 3D capabilities of WPF were utilized by Electric Rain in their next-generation presentation software, StandOut.

Monday, August 11, 2008 2:30:06 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

VS2008 SP1 RTM is now on MSDN Subscriber downloads.

It's an 830MB download, unlike the gigs and gigs of the actual product. But of course, it's still pokey because there's quite a bit of competition on the wire!

Two worthy additions:

1) Don't forget that the Silverlight .NET 2.0 Beta Tools for VS will be refreshed shortly. Current bits do not work with SP1. Here is the patch but DON'T MISS Amy Dullard's blog post, Silverlight Tools Must be Updated After Installing Visual Studio 2008 SP1, for information on some issues and non-English versions.

2)If you have previous installations of SP1 betas etc, you'll want to run this "preparation tool" to clean up your system prior to installing the RTM bits.

Monday, August 11, 2008 10:32:57 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 
 Friday, August 08, 2008

Don Kiely has been preaching the evils of Full Trust in .NET, SQL and Windows for quite a while. SO when he saw a question on the ASP.NET Forums on using the Dynamic Data Controls with EF as the data provider that involved a trust issue, he was on it like white on rice.

Dynamic Data with the Entity Framework in Medium Trust

The issue was specifically when you are using a website not a web application and you have the EDMX right in the site. In this scenario, the model files are always embedded into the sites assembly. You don't have an option to build the files externally. In fact, Embed in Output Assembly is the only option for Metadata Artifact Processing.

While one of the suggested solutions was to create the model in a separate dll so that you can build the model files separately from the DLL, Don show's how to easily use the EntityDesignerBuildProvider to get at the files.

Nice!

Friday, August 08, 2008 1:56:18 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 

Worth repeating from Guy Burstein's blog. I've followed some issues people are having with this on an email list. VS2008 SP1 will release "in about a week from now", or sooner, or later, but it is very close. So hang in there.

 

Sql Server 2008 Visual Studio SP1

Certain SQL Server 2008 features install components that are also part of the release version of Visual Studio 2008 SP1. So, if Visual Studio 2008 without the service pack is installed on your machine, you cannot install SQL Server 2008. If you try to install, you will ran into the following error:

Rule "Previous releases of Microsoft Visual Studio 2008" failed.

A previous release of Microsoft Visual Studio 2008 is installed on this computer. Upgrade Microsoft Visual Studio 2008 to the SP1 before installing SQL Server 2008.

In case you are wondering, Visual Studio 2008 SP1 should be expected about a week from now, so you don't have to wait too much.

Friday, August 08, 2008 1:31:49 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [0]  | 
 Thursday, August 07, 2008

This may only mean something to about 10 people on the planet, but I thought I'd share it anyway.

If you are using Entity Framework's MetadataWorkspace to dig around in the model's structure, the CSpace (schema from the conceptual layer aka CSDL) is readily available however, the other collections, SSpace (ssdl) and CSSpace( msl) are not loaded until they are needed. Well, trying to read them with the MetadataWorkspace doesn't count as being needed. They are needed during query compilation, then again at object materialization.

Unfortunately, there's not method to just say "hey, get me the MSL schema, please", though there are some factory methods deep down in the API that are a PIA to use.

SO instead, what I've been using is just a quick method that creates an ObjectQuery and then forces query compilation by requesting ToTraceString. Then throw away the query. No need to execute it.

I created this method in a Partial Class for a oarticular EntityContainer class.

Private Sub forceDataSpacestoLoad()
  Dim tracestring  = CreateQuery(Of Contact)("AWEntities.Contacts").ToTraceString  'the ESQL is a shortcut
End Sub

Now when I need to use the mapping or store collections, if the one I need is not loaded, I can force it.

If Not mdw.TryGetItemCollection(DataSpace.CSSpace, mappingCollection)Then
forceDataSpacestoLoad()
End If

Once loaded, ItemCollections stay in the application cache.

You could create something more generic and make it an extension method of ObjectContext. HEre, I just grab a random entity to build a query from and cast the whole thing back to an EntityObject.

   If Not randomentityType Is Nothing Then
     Dim esetName = mdw.GetEntitySetFullName(randomentityType)
     Dim tracestring = CreateQuery(Of EntityObject)(esetName).ToTraceString
   Else
     Throw New InvalidOperationException("Could not force metadata to load")
   End If

Now you'll probably want to know where GetEntitySetFullName comes from. It's one of the many extension methods I've created while writing my book.

Here ya go!

  <Extension()> _
  Public Function GetEntitySetFullName(ByVal mdw As MetadataWorkspace, ByVal entityName As String) As String
    Dim entContainer = mdw.GetItems(Of EntityContainer)(DataSpace.CSpace).First
    Dim entsets = From eset In entContainer.BaseEntitySets _
                  Where eset.ElementType.Name = entityName
    Dim containerName = entsets.First.EntityContainer.Name
    Return containerName & "." & entsets.First.Name
  End Function
Thursday, August 07, 2008 3:54:38 PM (Eastern Standard Time, UTC-05:00)  #     |  Comments [1]  | 
 Wednesday, August 06, 2008

Even though I seem to have gotten it right when I wrote about this in my book (I just checked) in my own memory I have had a misconception about GetObjectbyKey.

I thought that GetObjectbyKey and TryGetObjectbyKey only searches the cache for entities that already have been created. But I realized this morning that if it doesn't find the entity in the cache, it will create a query and go look in the database also.

dim myEntity=context.GetObjectbyKey(myKey)

or use one of the methods for creating an EntityKey on the fly.

Dim cust=context.GetObjectbyKey(new EntityKey("NorthWindEntities.Customers","CustomerID",3)

You can easily make this a generic method if you needed to.

Compare this to a query

Dim cust= (From c in context.Customers Where c.CustomerID=3).FirstorDefault

or

Dim cust=context.Customers.Where(Function(c) c.CustomerID=3).FirstorDefault

Using GetObjectbyKey vs. a query is not just about coding because they work very differently.

When you execute a query, EF will always go to the database first and retrieve whatever it finds. As it's loading the results into the cache, if the entity already exists in the cache, it will refresh that entity based on the MergeOptions.

  • If MergeOption is AppendOnly (this is the default) then the newly retrieved entity will disappear into the ether.
  • If it is OverwriteChanges, then the values from the server will replace the current values of the entity in the cache.
  • If it is PreserveChanges, the values from the server will replace the original values of the entity in the cache. The current values won't be touched.
  • MergeOption can also be set to NoTracking. That's an interesting scenario because it will create a second instance of this object. I'm not sure how I feel about that yet. However, if you already have an entity in memory but not in the cache, GetObjectbyKey will have the same effect. It won't find the entity in the cache and will go get one and put it into the cache - result is two instances of the entity.

So depending on your needs, GetObjectbyKEy (and TryGetOBjectbyKey) is very efficient because it won't go to the database unless it has to, however it won't refresh your data from the server as a query would.

Good to know so I can make the right choice when the time comes.

Wednesday, August 06, 2008 8:05:19 AM (Eastern Standard Time, UTC-05:00)  #     |  Comments [4]  |