Saturday, April 28, 2012

Web API and Entity Framework

Wow, I can't believe how long it has been since I've blogged.  This post is purely so that 3 weeks from now, when I have these same problems and Google/Bing for the answer, maybe I'll find my post first.

I've been working with the beta bits for Microsoft's Web API that shipped with the ASP.NET MVC 4 beta.  (You can get more recent, nightly builds, but I haven't been doing that.)  In any case, I've come across two problems that have bitten me repeatedly.

Server Generated Primary Keys

The first is a problem with how Entity Framework handles Server Generated primary keys. (This is not specific to Web API.) In my case, I'm using GUIDs and I have the server create a GUID every time a record is inserted.  The problem is that EF ignores the fact that the server is supposed to be creating the GUID.  The first time you insert a new record, you get a GUID with all 0's.  (00000000-0000-0000-0000-000000000000)  That record will actually insert, but the second time it obviously fails because the key isn't unique.

I found a good blog post about the problem here.  Through additional research on StackOverflow, I finally found that Microsoft had issued a hotfix.

Once you install this hotfix, you can open your EDMX and right-click on the PKs for every entity to expose the  StoreGeneratedPattern property.  Once you change this to Identity for each PK, you should be able to insert records and get the server generated GUIDs.

Navigation Properties

I wrote this blog post because I completely forgot about this problem and late last night blew away the EDMX and all the model classes in order to refresh everything after I'd made some major changes to the underlying schema.  After I did that, my Web API app stopped returning any data.

When using the  "ADO.NET DbContext Generator" tooling you get classes for each entity that include Navigation Properties that are declared as virtual in support of lazy loading.  We're not going to use lazy loading in Web API, so we can just remove the virtual keyword and everything works fine.

You could manually edit all the classes created by the DbContext Generator, but the next time you edit the EDMX and save it, all your classes will be regenerated with the virtual keyword. 

modified the file to stop adding the virtual declaration.  This is the original code (line 253 on my machine) that you'll want to change:

string PropertyVirtualModifier(string accessibility)
   return accessibility + (accessibility != "private" ? " virtual" : "");

I've only scratched the surface, but so far, I'm really liking Web API.  These two things are the only things that have really caused a lot of pain and once I found (and then later remembered) the solutions, they haven't been so bad.

Labels: , , , , ,

Tuesday, March 10, 2009

Did Windows Defender break ASP.NET Development Server?

I was maintaining a ASP.NET application late yesterday and made good progress down my to-do list.  My eyes were getting tired, so I decided to bump up the fonts in the IDE.  I'd never done this in Visual Studio so it took me a few minutes to find the right place to make changes.  In the process I discovered a ton of other options and probably spent 1/2 an hour just looking through all the things I could customize.  Eventually I went back to work and was ready to test the site.    I pressed F5 and the dev-web-server fired up like always but then... nothing.  Empty browser.  

The app built just fine, I hadn't made any changes to any of the config files and the last round of untested code changes were minor.  I couldn't imagine how, but I figured I must have done something in some option somewhere that hosed me up.  I looked at everything and set it back - even the fonts.  No go.  I decided that I got myself into trouble because it was too late so I called it a night.

The break of dawn didn't help.  I searched all over the Internet and tried a dozen things including completely uninstalling and reinstalling Visual Studio since at least one other person solved a similar problem this way.

I must confess that I did see the solution earlier in the day, but couldn't imagine it was the solution so I ignored it.

This evening I saw that same solution again on Experts Exchange.  It caught my attention this time because the thread was from today and referenced some of the same windows updates that I had noticed.  

Windows Defender installed an update yesterday that appears to have caused this problem.  Even after disabling Windows Defender the web server would not work.

In the end it was as simple as firing up notepad (as administrator) and editing the hosts file.  (C:\Windows\System32\drivers\etc\hosts)

Mine was vanilla as installed by Microsoft when I installed Vista last summer.  There was only one line in it:

::1 localhost

This line is apparently for IPv6 and hasn't caused any trouble in over 6 months.  Based on the Experts Exchange recommendation (which matched one I saw earlier)  I changed it to the standard localhost

and just like that all is good in my world again.  

Thanks, Microsoft!  I'm almost back where I was 24 hours ago.  ;-)

(In hindsight, I really should have tried the System Restore I was considering last night.  What on earth was I thinking.)

Labels: , , ,

How to change the default browser in Visual Studio 2008 with ASP.NET MVC apps

This is the third time I've had to figure this out and it doesn't seem to be posted anywhere so I thought I'd post it myself for the next time I need to find it.

You'll find many tips that say "right click on any .aspx page" in solution explorer and chose "browse with"...

As it turns out, that doesn't work in a MVC project.  If you right click on any page other than default.aspx you will not get the "browse with" option.

So, when working on an ASP.NET MVC application, right click on default.aspx and chose "Browse With..."  Then you'll get a dialog that will allow you to change your default browser.

Labels: , , ,

Saturday, November 1, 2008

Pass the blame around

Have you ever wondered who was to blame? Have you ever felt like things would be OK if you just knew who to blame? Have you ever needed to pass the blame? Well, thanks to the latest technology, you can just Blame it on Rick. has come to the rescue. Just launched yesterday, this brilliant, cutting edge, highly innovative Web site lets you blame anything you want on Rick. The site handles Business, Personal and Volunteer issues with equal dexterity.

The best thing is that using Blame it on Rick yields the same exact results as in real life - nothing! Sure, you can feel better knowing that someone else has the blame; but otherwise, the issues remain unchanged.

The only downside is that the site seems to be processing things a bit slowly. (And when you need someone to blame, results just can't come fast enough.)

Once you submit the blame, you'll get your request number. If you jot it down, you can check on the status of your blame request using the simple intuitive Status feature.

The site has accepted over 3000 requests for blame, but is currently processing request number 629. (It must be a very difficult request, it has been in process for hours!) Be patient though, and you can feel comfortable knowing someone else is to blame.

I expect that in the future, the site might offer some deep analytical reporting of important key social metrics, like Who does the most finger pointing, and for what issues are people least likely to accept responsibility. I'd also like to see stats like blames resolved per hour and longest blame resolution and shortest blame resolution.

Just wait until the politicians find this site!



Thursday, October 30, 2008

Alan Stevens - Manage Complexity with Agility

This session rocked!

Alan started out by pointing out that he is not an expert, just an enthusiast. His enthusiasm was self-evident and contagious.

He said that this session is about what has worked for him and encouraged us to avoid dogma and take what works for each of us.

The following are my key take-aways from Alan's session:

Iterations are about putting a heartbeat to your practice and about creating a constant velocity. (Don’t push out a whole bunch of stuff one month and nothing the next. ) These are great things to keep in mind. Here I am writing this weeks later realizing just how much I need to put this into practice.

Alan's Law
“Heroism is Failure” If you have to be a hero on a regular basis, something else is broken.

Short Horizon
If you're working with regular 2 or 4 week iterations, you have an emotional advantage: You know what you’re dealing with. If you're coding for months on end with no iterations, you can feel lost. Also, it gives you the chance for Frequent Course Corrections. Two to four weeks worth of “wrong work” is easier to correct than 6-9 months.

User Stories
Alan emphasized two points. First, user stories are Users' Goals in Users' Terms. They are not technical requirements. They are not about software or technology. They are the essence of the value they need us to deliver.

For developers, it is a reminder of a future conversation – a to-do list.

Alan also suggested the following user stories template:
As a : (Role)
I want (something)
So that (benefit)

User stories allow us to avoid BDUF – Big Design Up Front.

The Last Responsible Moment
Defer decisions until the last responsible moment because that is when we have the most information. Don’t pretend you know things you don’t.

The next portion of Alan's session covered testing and dependency injection and he showed FoxUnit and FoxMock. I saw Alan cover these topics in very good detail last year at Southwest Fox and FoxForward.

Alan started his discussion of refactoring by asking "if" we refactor and what we do when we refactor. It seemed that the audience had a pretty good handle on refactoring. Alan reviewed some common elements of refactoring such as Code Smells. (a hint that something might be wrong in the code)

Alan made the point that you should Enable Change – if you’re afraid to change a piece of code, that’s a smell.

I jotted down a few additional quotes that I thought were worth sharing:

Work on your business not just in your business. - Michael Gerber
Value Developer Cycles over CPU cycles
Eliminate Waste
Prevention is the best cure.
Avoid Complacency

Finally Alan recommended a couple books that I need to add to my reading list:
The Pragmatic Programmer – Andrew Hunt and Dave Thomas
The Goal – Eliyahu M. Goldratt and Jeff Cox

This was one of the best sessions at Southwest Fox this year. I mentioned to Alan that I didn't think he needed to show any code and he countered that it is really important to show people how to test - that talking about it isn't enough. He's probably right about that.

What this session did for me was to help me focus on the bigger picture of best practices. There was a communal enthusiasm in this session - we all wanted to go out and code better, immediately.

Labels: ,

Sunday, October 19, 2008

Toni Feltman - Square Peg, Round Hole - Retrofitting Client Server Data Access into Legacy FoxPro Applications

Toni started by letting us know that this session was for people working with VFP apps (not 2.x apps) that need to move from a VFP back end to some other back-end, such as SQL Server, Oracle or any other back-end.

Toni pointed out that it is important to set goals. Up front, the team needs to agree on why you're changing back-ends, what you hope to achieve, and a timeline - taking into account things that are likely to cause you trouble.

Toni suggested that once you pick your data source (And It think maybe even as part of the decision making process) you should become well versed in the tools that you'll be able to use to manage and manipulate your new data source.

Related to that, Toni strongly suggested hiring an expert to get your new database up, running and properly configured. I think this is good advice as even after working with SQL Server for six years I know next to nothing about proper configuration or performance tweaking.

Next, Toni discussed the issue of actually moving the data. She discussed the various approaches of moving or converting the data such as VFP's Upsizing Wizard. (This was mentioned twice this week as being a good tool for moving data from VFP to SQL Server. Toni mentioned that several of the third party tools can convert data much faster than a custom-written VFP app. I didn't realize this and I hope I remember it on my next conversation. We've had several in the past that were very time consuming.

Toni also pointed out that this is the time to decide whether or not to redesign the database. She correctly points out that we rarely design a database correctly the first time, nor is it likely that changes applied to our databases have been done according the best practices. If you have a reasonable timeline, this should be considered an opportunity to redesign your database. If you're on a short timeline, you're likely to need to create an identical design in the new back-end.

Toni reviewed the different approaches we can take to access our new back end such as Remote Views, Cursor Adapters, SPT, and Stored Procedures. She walked us through code examples all but Stored Procedures.

Toni suggested several approaches for getting reacquainted with our legacy code so that we can look for the things that will need to change for the new back end.

Perhaps one of the most important issues Toni raised was that you must have a reproducible process for data conversion. You're going to need to convert over and over as you retrofit your code. You'll need to be able to get clean, live, production data into your new back-end on a regular basis. This is something I also learned from working with Rick Schummer and Steve Sawyer and I couldn't agree more. Nearly every application we develop starts with writing a conversion routine.

Toni wisely instructed us to make sure you can completely disconnect from Production Data – hard coded paths and things like that can really bite you. Toni suggested setting up a virtual machine without a connection to the network drives.

Then, Toni suggesting reviewing you code for commands that are specific to you old style of data access such as USE and SET ORDER. (Using Andrew MacNeill's Code Analyst from VFPX will probably make this a breeze.)

Toni also discussed the very important issues relating to how different back-ends store data differently than VFP, particularly when dealing with NULLs, empty strings, logical, (and I'd offer dates, too.) I have to admit, that I've changed code to handle these issues instead of taking Toni's much smarter approach of creating a library of methods to handle this data once instead of each time you come across it in your code. (I think her approach would also make it easier for you to retrofit a vertical market app for multiple back-ends.)

Toni also mentioned that you should start testing right-away - as soon as you have something to test. This will help to uncover problems earlier in the process instead of later when it will be more costly to address.

She also suggested that you should really try to avoid new development during this process. Adding new features should be pushed off until after the migration is successful.

As usual, Toni gave a well prepared and well presented session with great ideas.

Southwest Fox 2008, Mesa, AZ, October 16 - 19, 2008

Labels: ,

Friday, October 17, 2008

Soutwest Fox - Day 1

I've been taking detailed notes in each session and will be blogging soon about the sessions I've attended today. Overall, they have been top notch. I had hoped to have a little more of a live stream of posts, but the content has been pretty think so I've only had time to take notes, not make them web-ready.

So far the highlight for me has been Alan Stevens' session, Manage Complexity with Agility.

One overall positive comment: Each room has giant screens and each projector is set to use the full screen. I've been sitting in the back row for some sessions (to get power) and I've been able to see every line of code shown. This conference is doing this better than any conference I can remember in recent history. Because the screens are so large, some of the slides and windows have been down below the horizon of people in chairs. Usually the speakers remember to move thing up to the top.

Two more sessions to go and then perhaps some time to post some content-filled updates.

Labels: ,