Julia Lerman's Blog, page 16

April 6, 2013

Getting Started with WCF Data Services 5.4, OData v3 and JSON Light

TLDR: Setup steps to get WCFDS 5.4 up and running


JULBS (Julie’s Usual Lengthy Back Story):


WCF Data Services (aka WCFDS) updates are being released at a rapid pace. I think they are on a hoped-for 6-week release schedule and they just released 5.4 a few days ago.


One of the reasons is that they are adding in more and more support for features of OData v3. But rather than waiting until ALL of the features are in (by which time OData v4 may already be out! :)) they are pushing out updates as they get another chunk of features in.


Be sure to watch the http://blogs.msdn.com/astoriateam blog for announcements about release candidates (where you can provide feedback) and then the releases. Here for example is the blog post announcing the newest version: WCF Data Services 5.4.0 Release.


I wanted to move to this version so I could play with some of the new features and some of the features we’ve gotten starting with v 5.1 – for example the support for the streamlined JSON output (aka “JSON light”) and Actions & Functions.


As there are a number of steps involved for getting your WCFDS to use v 5.4, I somehow performed them in an order that left me in the cold. I couldn’t even get JSON light output.


So after scratching my head for way too long, I finally started the service from scratch and this time it worked.



Create a new project to host your service. I recommend a WCF Services Application project
Delete the IService1.cs and Service1.cs files created in the new project.
Add a new WCF Data Service item
Remove the references to the following WCFDS 5.0  assemblies:

Microsoft.Data.Edm
Microsoft.Data.OData
Microsoft.Data.Services
Microsoft.Data.Services.Client
System.Spatial


Using Nuget, install WCF Data Services Server. The current version available on Nuget is 5.4.

When you install this, Nuget will also install the other 4 assemblies (Edm, OData, Services.Client and System.Spatial)
UPDATE! Mark Stafford (from the WCFDS team) let me in on a “secret”. The WCFDS template uses Nuget to pull in WCFDS5 APIs, so you can just use Nuget to UPDATE those assemblies! (this is for VS2012. For VS2010, use the crossed out steps 4 & 5 :))

image



Install Entity Framework 5 from Nuget

That should be everything you need to do to have the proper version of WCFDS.


Now you can follow a normal path for creating a data service.


In case you’re not familiar with that the basic steps (just getting a service running, not doing anything fancy here)



Add reference(s) to any projects that contain your data layer (in my case a data layer with EF’s DbContext) and domain classes.
Modify the WCFDataService class that the Add New Item wizard created so that it uses your context. My context is called CoursesContext, so here I’ve specified that context and simply exposed all of my entity sets to be available as read-only data using the SetEntityDataAccessRule. That is not a best practice for production apps but good enough to get me started.


public class MyDataService : DataService<CoursesContext>
{
public MyDataService()
{
Database.SetInitializer(new CoursesInitializer());
}



.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }



public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
config.UseVerboseErrors = true;
}
}



.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


 


My data layer includes a custom initializer with a seed method so in the class constructor, I’ve told EF to use that initializer. My database will be created with some seed data.


Notice the MaxProtocolVersion. The WCFDS item template inserted that. It’s just saying that it’s okay to use OData v3.


I added the UseVerboseErrors because it helps me debug problems with my service.


Now to ensure that I’m truly getting access to OData v3 behavior, I can open up a browser or fiddler and see if I can get JSON light. JSON light is the default JSON output now.


WCFDS now lets me leverage OData’s format parameter to request JSON directly in the URL. I can even do this in a browser.


Chrome displays it directly in the browser:


image


Internet Explorer’s default behavior for handling a JSON response is to download the response as text file and open it in notepad.


In Fiddler, I have to specify the Accept header when composing the request. Since JSON light is the default, I only have to tell it I want JSON (application/json).


image


Fiddler outputs the format nicely for us to view:


image


If I want the old JSON with more detail, I can tell it to spit out the odata as “verbose” (odata=verbose). Note that it’s a semi-colon in between, not a comma.


image


(I’m only showing the JSON for the first course object, the second is collapsed)


image


Checking for the existence of the JSON Light format and that it’s even the new JSON default, helps me verify that I’m definitely using the new WCFDS APIs and getting access to the OData v3 features that it supports. Now I can go play with some of the other new features! :)

 •  0 comments  •  flag
Share on Twitter
Published on April 06, 2013 07:38

April 2, 2013

A Notable Entity Framework Performance Eater: Casting nchar to nvarchar under the covers

Brian Sullivan was having a weird query performance problem with one particular EF query and asked about it here on Stackoverflow. Some very knowledgeable people asked all the right questions. It made no sense.

He finally discovered the source of the problem himself.

His code first model defined a string property which SQL Server automatically presumes is an nvarchar. But his database had an varchar. Since these are coercible, Entity Framework decided to coerce the type under the covers. And it was very expensive. Brian fixed the problem by applying a mapping that just said “hey EF, that’s an varchar by the way” and all was well again.

This is one of those awful problems that will be hard to solve and hard to do a web search for. But hopefully the SO question or this blog post will help the next dev coming down the pipe.

 •  0 comments  •  flag
Share on Twitter
Published on April 02, 2013 13:17

March 16, 2013

Video Pick from Getting Started with Entity Framework 5 Course

Here is a 6 minute preview from my new Pluralsight course, Getting Started with Entity Framework 5.

 

The course is about 4.5 hours long and I’ve written a small “guide” to it in this blog post: Getting Started with EF5 – New Course on Pluralsight so that you can decide if the course is for you or not. But I think it’s for everyone!! :)

Or you can go right to the course here: http://pluralsight.com/training/Courses/TableOfContents/entity-framework5-getting-started

Or you can watch one of my 9 other Entity Framework courses on Pluralsight! :) http://pluralsight.com/training/Authors/Details/julie-lerman

I’ve still got a short stack of 30 day trial cards for Pluralsight if you need one but you have to promise to use it to watch my courses! :) (And any of the other awesome courses you can find there as well.) Send a message to me through my contact form and I can shoot you the code.

1 like ·   •  0 comments  •  flag
Share on Twitter
Published on March 16, 2013 14:02

March 10, 2013

Entity Framework at Boston Code Camp #19

I had the pleasure of going to Cambridge this weekend along with a car load of Vermonters (myself, Rachel Reese, Dennis Doire and Kyle Mitofksy) to participate in Boston Code Camp at Microsoft’s NERD Center.

We left just as Boston was getting pummeled with snow and a prediction of 10-14”. We saw nary a flake on the drive down.

Rachel gave two F# presentations. Dennis gave a session on Specflow and Selenium. I did two talks on ..guess what? Yep Entity Framework.

 

Session #1 Entity Framework in the Enterprise

The first talk was Entity Framework in the Enterprise. It was a very compressed version of my 3+ hour course of the same name on Pluralsight: Entity Framework in the Enterprise.

You can find the slides for this session on Slideshare at http://www.slideshare.net/JulieLerman/lerman-ef-in-the-enterprise-bocc13.

I spent quite a lot of time preparing and testing the 6 solutions that I showed in the session. Afterwards a friend noted that I had never actually run any of the apps or test whose code I had spent most of my time exploring. That made me laugh because I had worked hard to ensure that every app ran correctly and every test passed. So to remedy that, I have created a short video on YouTube where you can actually see the tests run (and pass) and the one of the apps running. Here’s that video: Lerman EF in the Enterprise Run Tests BOSCC13 (there’s no sound and it’s about 4.5 minutes).

Session #2: EF FTQs (or Entity Framework, Frequently Tweeted Questions)

I have also put up the slides for the second session on Slideshare at http://www.slideshare.net/JulieLerman/julie-lerman-entity-framework-ftqs-frequently-tweeted-questions. Something strange happened during the demos. I wanted to show attendees the reverse engineering feature of the EF Power Tools. I pointed to a database and nothing happened…no classes were created. I’ve never seen that happen before. I didn’t see an error message on the bottom of the screen. I tried it again with the same results, so instead of doing what I wanted to which was get to the bottom of the problem, I knew it was better to just move on. I spent some time this morning trying to duplicate the problem and never could. So who knows what happened. So for those of you who haven’t seen this feature before, I recorded a little video (again, no sound and it’s only 78 seconds) so that you can see it in action. That’s here: Lerman Reverse Engineer Database for Code First

It was a great code camp! Congrats to all of the folks who made it happen: speakers, attendees, sponsors and organizers/volunteers…especially to Patrick Hynds, Bob Goodearl and John Zablocki and Chris Pels.

bostoncodecamp.com

1 like ·   •  0 comments  •  flag
Share on Twitter
Published on March 10, 2013 18:15

March 6, 2013

Getting Started with EF5 – New Course on Pluralsight

While EF5 did not bring a huge amount of change to Entity Framework, one small change in the EF Designer in Visual Studio 2012 made a huge difference for people getting started with Entity Framework. The designer now uses a code generator that spits out POCO entity classes managed by a DbContext by default rather than the EntityObject based classes managed by the ObjectCpntext as it did for VS2008 and VS2010. It’s a VERY good change but because almost all of the training materials out there for EF newbies is based on VS2010, it is pretty confusing to get started with EF5 and VS2012.

The advanced materials are still completely valid.

While this is driving me a little crazy with respect to the 2nd edition of Programming Entity Framework – a huge undertaking to update, I was able to fix the problem by creating a brand new course on Pluralsight.

It’s just been released today and is called “Getting Started with Entity Framework 5”.

 

It’s got 6 modules.

Entity Framework 5: Planning Ahead

This is a good overview for newbies but also highlights “what’s new” which will be really helpful for people coming from EF4 or even EF4.1, 4.2 or 4.3.

Database First Modeling

Like using the designer and starting with an existing database? Database First is for you. Also, there are plenty of new designer features so if you’re experienced with EF but moving to VS2012, you’ll want to learn about the new features (including creating enums from database fields and supporting geography data types).

Model First Modeling

Like the designer but no database yet? You may prefer to go this route of designing your model in the designer and letting the designer generate database schema for you. While I touch on some of the new designer features in this module, I don’t pay as much attention to them as I do in the DB First module because it would just be much too repetitive. But you will also see here how to define enums and geography data that will properly map the database schema that the designer creates.

For both model first and database first, you can learn more about customizing your model in the course Designer Supported EDM Customization.

Code First Modeling

If this is you: “eew designers stink” then you probably want to just use code to define your model. This gives highlights of Code FIrst along with the new enums & spatial data support. You can then continue your education in the EF 4.1 Code First course and the Code First Migrations course.

Interacting with your Data Model

Whether you’ve created your model with Database First, Model First or Code First, using the model is almost always the same. (“Almost” because code first does not yet support mapping to stored procedures so that’s the only real difference.) This module uses a simple console application to get you going with the basics of querying and updating your database using the DbContext. Once you’ve got an understanding of these basics, the dedicated DbContext, Querying and Validation courses that I’ve created in the past few years will get you the in-depth info you need for more advanced work.

Using EF in Your Solutions

I didn’t want to leave you with the impression that what you saw in the previous module is as far as EF can go. This last module introduced you to some good architectural practices and then I use EF (along with these basic architecture patterns) in a client side application (WPF), a web app (MVC) and a data service so you can see how the pieces fit together. I have a much more advanced course called “Entity Framework in the Enterprise” that you can watch after this. That course was created using Visual Studio 2010 and Code First and the DbContext and everything in there will be applicable to EF5.

Enjoy!

http://pluralsight.com/training/Courses/TableOfContents/entity-framework5-getting-started

 •  0 comments  •  flag
Share on Twitter
Published on March 06, 2013 09:01

February 4, 2013

What’s Best for Unit Testing in EF? It depends, dude!

This tweet stopped me in my tracks because I couldn’t reply in a tweet:

Stephen Coulson @sdcoulson

@julielerman In your opinion what is the best solution for unit testing#EF dependent code?#MockingFramework? #SQLCE? or other?

It’s a loaded question so I’ll just answer briefly.

First: it depends! :) It always depends.

Do you mean unit tests or integration tests? A unit test shouldn’t be hitting a database.

FAKING FOR UNIT TESTS

I use fakes to avoid hitting a database when I truly want to do a unit test on code that’s trying to do data access. I (and many others) have written extensively about faking with EF. I have some old blog posts and book chapters that do this with EF4 but not using code first or dbcontext.

In my DbContext book I have some examples of faking with DbContext (ala EF4.1 & EF5).

I have a module in one of my Pluralsight courses on testing with EF (Entity Framework in the Enterprise).

MOCKING FRAMEWORKS FOR UNIT TEST

I’ve had a hard time finding good current examples of using mocking frameworks with EF. Typemock has one that’s terribly outdated and not useful. I found one person who’s written a little about using Moq with EF.

SQL CE FOR INTEGRATION TESTS

Sure you can use SQL CE. It’s a perfectly good way to go. Here’s a blog post by Eric Hexter to get you started:

Using sql compact for integration tests with entity framework

CODE FIRST DATABASE INITIALIZATION FOR INTEGRATION TESTS

This is what I use. One of the intializers is “DropCreateDatabaseAlways”. I use that combined with some seed data to refresh my database for each integration test. This is one of the methods I show in my Pluralsight course.

More More More!

There are plenty of other ways to go as well. For example, Lynn Langit has pointed me via twitter to ApprovalTests.

And just watch the comments fill up with other suggestions.

Hope this helps, Stephen! :)

 •  0 comments  •  flag
Share on Twitter
Published on February 04, 2013 07:43

January 26, 2013

Attaching an MDF Database to LocalDb in Visual Studio the Easier Way

I’ve been loving Visual Studio 2012’s SQL Server Object Explorer (SSOE) instead of always jumping out to SQL Server Management Studio (SSMS) to do database management. SSOE covers many (not all) common tasks…and many more than VS’s Server Explorer let’s you do.

But I spent an embarrassing amount of time trying to figure out how to easily connect an MDF file to my SQL Server 2012 LocalDB instance because, duh …I was doing it wrong! Over and over and over again.

SSOE lets you inspect databases that are already attached to localdb and it lets you create new localdb databases. I couldn’t figure out how to attach to an existing database.

Finally, some brain cells returned to my skull and I went over to VS’s Server Explorer and attached the database there (and provided a nice logical name rather than ending up with the rangy file-path-as-database-name).

Once it was attached to localdb, then I could see it in SSOE and actually access it in my app using a normal connection string.

Just “duh” for how much time I spent on this.

It was important to me because I need to distribute sample solutions along with existing databases and I want the database files in the solution folder and *simple* steps I could relay to users to make sure they could use them when they want to debug and test out the solution themselves. I’m perennially looking for a simpler way to do this. I’ve used SQL Express, SQL CE, database scripts, packaged exes, you name it. And I always spend hours trying to do it in a way that it’s really simple for the dev who is trying out my sample solution.

Oh and one more trick for you about localdb

I have always had a problem remembering forward slash vs. back slash. Here’s a blog post from 2006 as evidence! So connecting to localdb is always a challenge because you specify it as (localdb)\v11.0.

My little secret to get the back slash correct is visual:

image

I know it’s totally dorky, but I just make sure that the slash is parallel to the first part of the “v”.  Hey, it works for me, what can I say? :)

 •  0 comments  •  flag
Share on Twitter
Published on January 26, 2013 13:34

January 3, 2013

April Conferences Where I’ll be Teaching, Plus a Workshop

I’ve enjoyed staying home in Vermont for most of 2012 but the wanderlust is setting in. I’m scheduled to speak at two conferences in April:

DevIntersection April 8-12, Las Vegas

I’ll be doing 3 talks at this conference, but am not exactly sure which ones will be selected yet. And yes, there will be some entity framework involved. :) This is going to be a great conference and I’m excited to be included in a great lineup of speakers!

Database Days, April 16-18 in Baden Switzerland

*Full Day Workshop April 16th : Hard Core Entity Framework for Enterprise Developers
During the conference proper, I’ll do two talks:
*One for developers: Entity Framework Code First Migrations
*One for DBAs: Entity Framework & Your Database: Not as Evil as You May Think

While at Database Days, I’m looking forward to seeing Baden, a beautiful and historic town.

 •  0 comments  •  flag
Share on Twitter
Published on January 03, 2013 12:37

December 19, 2012

My new rig (aka a development box for a geek who is not a hardware nerd)

This fall I got myself a new development computer. I did my usual indecisive Libra thing over for months before settling on something.

My last machine was one that I got arm-twisted into building myself. I’m just not made for this. It should have been obvious when the parts list I selected from Newegg did not include a processor. When the parts arrived they sat in their boxes for 6 weeks. When I finally put it together it took me over 8 hours. And it never worked right. Ever. Even though I spent a lot of time over the next two years trying to figure out and fix it’s problems.

For this NEW machine, I gave it the old college try, having learned from my earlier experience and came up with a parts list that someone kindly pointed out had a motherboard and a case that were incompatible. I made many lists over the course of 4 weeks.

Finally another geek friend told me what I wanted to hear …just get a Dell and be done with it. That’s what I have normally done. This time I did buy it with an advance plan to customize it a bit. So here’s what I did.

I started with a Dell Optiplex 9010. It is a good workhorse for development.

That has an eco-friendly option of a low powered power supply which I was happy to select. I also went for the mini-tower which gave me enough flexibility to add in an extra drive.

The specs I chose were:

Processor: 3rd Gen Intel Core i7-3770 Processor (8MB, 3.4GHz) Memory: 4GB Graphics Card : (Proprietary) 1GB AMD RADEON HD 7570 without Adapters (supports 1 DVI and 1 Display Port) Drive: 1TB SATA  O/S: Windows 7 Professional,No Media, 64-bit DVD: 16X DVD+/-RW SATA, Data Only, OptiPlex 901 No mouse, no keyboard, no monitor

Thanks to whatever deals were available at the time, this ended up costing $1050 plus tax and free shipping.

At the same time I ordered from elsewhere:

Crucial C4 256 SSD drive (2.5” with adapter) Western Digital 2 TB Green Drive (not quite as fast as their Velociraptor but uses less energy :)) 3.5” 12 GB RAM from Crucial

I also ended up needing:

a DVI to Display Port adapter for my 2nd monitor (I ended up with this) an extra hard drive tray – proprietary to Dell and specifically made for that model. I should have ordered it with the computer! (for an extra $9 rather than $25 from dell after the fact or trolling ebay)

Putting it all together:

I took out the 1TB drive and it’s still on my shelf but will go to good use. I installed the SSD as my O/S drive and the 2TB drive as a secondary drive. I installed WIndows 8 pro on the ssd and split it in half. So 128 for the c: and 128 for d: which will be critical data that I use a lot. The rest of my stuff is on the 2 TB drive.

Note that this is not where I store things like photos and videos. This is just my development machine. I also have a laptop for email, writing, etc and a have an in house NAS that I use for storage and sharing.

I also installed the extra RAM so I have 16 GB. I have two DVI monitors plugged into this machine. One is plugged into the DVI port on the graphics card and the other into the display port on the same graphics card (via the DVI to DP adapter I bought).

The performance is great (if you ignore the graphics which are more than sufficient for my needs).

image

And it’s very quiet which is important because I record screencasts for Pluralsight.com. So quiet in fact that I started getting bothered by the clock ticking on the wall and had to move it!

Where I notice the difference in speed on this machine is when I’m rendering videos in Camtasia. I imagine some of this may be due to the fact that it’s also a new version of Camtasia, but the videos render SO fast. What used to take about 45 minutes now takes about 2 minutes. I’m sure that the RAM, the processor and the SSD all contribute to that.

So I’m happy with this and have been using it for a few months now and just wanted to share in case anyone is in my boat -- which is that I am NOT a hardware nerd and don’t have it in me to become one. And it did not cost me an arm and a leg. All told it was under $1500 which is relatively inexpensive for what I ended up with. Also, I need to consider that this is one of the few monetary investments I have to make for my business once every few years which makes it a bargain!

Sweating over all of the options will be the death of me some day. Thanks to the super-sensible Ryan Christensen-Schwarz (@mamanze) for talking sense into me. He was the one who said “oh just get a Dell”.

 •  0 comments  •  flag
Share on Twitter
Published on December 19, 2012 11:56

December 13, 2012

Beware machine.config files messing up Code First provider factories

It took us a long time and a bit of luck to get to the bottom of this problem. It turns out that at two completely different client sites they had computers whose machine.config file had been affected in a way that was causing an error in Code First.

The error that was being thrown at runtime was:

“Unable to determine the provider name for connection of type 'System.Data.SqlClient.SqlConnection'”

Yet the database was reachable from ADO.NET code in the app that used the same connection string.

This led us down a lot of exploration of the provider settings in the web.config file.

Finally a clever developer at the client site wrote a little debugging tool that helped us see the *true* error which helped him get to the bottom of the problem.

The real error was:

"'DbProviderFactories' section can only appear once per config file error'"

And it turned out that the machine.config had extra DbProviderFactories sections in it’s system.data element.

We are still trying to figure out what may have created this problem in machine.config but in the meantime, I’m trying to create some searchable text to help the next person who hits this problem.

I’ve ensured that the EF team is already aware of the problem.

Here is the MSDN forum thread related to this problem along with the solution:

Unable to determine the provider name for connection of type 'System.Data.SqlClient.SqlConnection'.
 •  0 comments  •  flag
Share on Twitter
Published on December 13, 2012 17:18

Julia Lerman's Blog

Julia Lerman
Julia Lerman isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Julia Lerman's blog with rss.