If you are working with LINQ to SQL, you may be familiar with this exception message:

“Cannot add an entity with a key that is already in use.”

This typically comes up when you are working with LINQ to SQL, as I am, in a “disconnected ” situation.  In my case, I am working with a Silverlight Application, so I get some data with one DataContext, then pass it out to my Silverlight application (this is the same with any other web application), make changes to it, pass it back to the server where it gets updated using a different instance of my DataContext.

The interesting thing in my case is that I thought I fixed the problem but I still got the exception.  I’ll explain why.  But first, there are several ways you can resolve this issue.  How you choose to fix this will have to do with how your database is set up initially.  I am working with an existing database and there are limits to how I can change the db.  So I have my all of the properties in my LINQ to SQL Entities set as “UpdateCheck = UpdateCheck.Never”.  Next I’ll “Attach” my entity to my DataContext  but before I do that, I’ll “Detach” it  My Detach() method “resets” all of the EntitySets and EntityRefs connected to my entity so that LINQ to SQL doesn’t think I am trying to insert them into the database.  Sorry, I’m not going into details on this process in this blog post but you can easily find details on the web.  Now I can attach my disconnected entity to my DataContext like this: 

            using (ABCDataContext db = new ABCDataContext())
            {
                foo.Detach();
                db.Foos.Attach(foo, true);
                db.SubmitChanges();
            }

This should work fine.  But it wasn’t working in my case.  Why?  It is a typical case of how various steps I took to resolve the a different problem ended up causing new problems.  I was debugging and trying a bunch of stuff because my calls to Attach were throwing exceptions.  There is an overload to the Attach() method that would look like this — db.Attach(newEntity, originalEntity)  — so that LINQ to SQL can compare the two items.  I had been playing around with it but when I commented out my code, I forgot to comment out one line, so it looked like this:

            using (ABCDataContext db = new ABCDataContext())
            {
                foo.Detach();
                Foo oldFoo = db.Foos.SingleOrDefault(f => f.FooId == foo.FooId);  // NOT REMOVING THIS LINE CAUSED THE EXCEPTION!
                //db.Foos.Attach(foo, oldFoo);
                db.Foos.Attach(foo, true);
                db.SubmitChanges();
            }

When I run this, I get the exception “Cannot add an entity with a key that is already in use”.  For some reason, even though I wasn’t working with “oldFoo”, LINQ to SQL didn’t like the fact that I requested it from the database.  So I commented out that line (Foo oldFoo = db.Foos.SingleOrDefault…) and everything just started working fine. 

It was a pain but it is over now.  Now it is time to solve more problems…

It’s been a while since my last entry of The Ben Chronicles.  As I look through my collection of photos I see why.  I have taken very few photos of Ben since Thanksgiving.  There’s a few reasons for that.  One is that my camera is on the fritz.  But hopefully some new rechargeable batteries will solve the problem.  The bigger issue is that Ben was sick a lot.  In December I think he was sick more days than he was healthy.  First a stomach bug (no details but that was fun), then  a cold, than another cold, than another, etc.  Most of these weren’t anything major but one time he gave us a scare when his hole throat puffed out.  Turns out it was just really bad swollen glands due to all of the colds he has had.  Anyway, he was sick during Rebecca’s entire winter break which was really a bummer.  Each time he goes back to daycare and then a few days later he is home sick again.  But he seems to be pretty healthy these days (or should I say, today).  People warned me that the first year at school or daycare is when kids get sick the most. 

When he is feeling good, Ben is a bundle of fun.  He’s really curious and he is really stubborn.  He loves to crawl around and play with stuff.  He loves being tickled and laughs a lot!  He’s not so interested in baby food any more and wants to eat “real” food.  He really likes to feed himself.  I think all of those sick days made him a little more needy because he wants to be held more than ever.  Or maybe it is just a phase he is going through.  He isn’t gaining a lot of weight these days, and all those sick days didn’t help, but he still seems to be looking a lot bigger.  As he gets close to 1 year old, he really is changing into less of a baby and more of a little boy.  But we still have a long way to go.

 

DSC03868 DSC03873 DSC03875 DSC03877 Baby Smash2

I’ve already blogged about how cool Live Mesh is, you can read my previous post here.  Well the other day I had a great idea to synchronize my Internet favorites for all of my machines in Mesh.  It was really easy to do.  Of course I am not the first person to have this idea and there are lots of blog posts out there already about how to do it.  Here is the basic idea… Just find the folder on your computer where you already save your favorites.  Add that folder to your Mesh.  Make sure your other machines are synced to that folder too (you can do that when you add the folder to your Mesh, or you can go to your Mesh Desktop and set it there too).  Then on your other computers, you can specify where you want the new folder to go.  Just set it to replace (actually you can merge the two of them) your existing Favorites folder on each machine.  If you have used Mesh at all you will see this is very easy to do.

Some other people have already taken the time to explain it in detail here, here, and here.

 

Technorati Tags: ,

As an ASP.Net developer, one of my favorite tools is Firebug.  Generally, my default browser is IE, but when I develop web sites I always debug and test with Firefox with the Firebug Add-on to make designing the pages easier. If you are a web developer and you aren’t familiar with Firebug you must check it out.  It basically allows you to inspect your page in the browser at runtime.  With Firebug you can see how your ASP.Net Code gets rendered into HTML, and how the CSS is applied.  Often it was not as I expected!  You can even modify the html, CSS and JavaScript at runtime.  This is a great help because you can tweak the page in the browser to look the way you want and then go back and apply the changes to your source code.

Now I am developing a Silverlight Application.  The learning curve is steep, working with XAML I am starting from scratch.  It isn’t like HTML at all and it is not so easy to make controls look like you want the to.  And it was even harder because I didn’t have Firebug to help.  Searching the Internet, I found Silverlight Spy.  This is a cool, free tool that helps in many ways with Silverlight Development.

With Silverlight Spy you can inspect your XAP package, monitor events, network activity and performance and more.  But the best part is that I can inspect the elements within my XAML at runtime and even make changes.  This is pretty important because I find that the XAML design experience in Visual Studio is less than perfect.  I’ve already used Silverlight Spy to help me solve a bunch of problems.  The tool isn’t perfect, but it is BETA software and it is free, so I am not complaining.  Many, many thanks to the developers.

 

Sometimes strange things happen.  It happened to me, I figured it was a fluke.  But last week it happened to my co-worker too.  Weird right?  Everything was working great with my VS Solution including a Silverlight App.  It isn’t new and I’ve debugged it many times.  But for some reason, no matter what I did, the debugger seems to be ignoring my breakpoints.  At first I thought that maybe the code wasn’t getting called, but it sure seems like it was getting called.  If you find yourself in this predicament, here’s all you need to do.   Go to the Property Pages of your Web App (the application that hosts the Silverlight).  You get there by right clicking on the Web Application in the Solution Explorer and select Properties.  Then choose the “Web” tab and you’ll see the one below.  If debugging doesn’t work, the “Silverlight” checkbox is probably not checked!  I’m not sure how this setting gets reset, but at least it is easy enough to fix.

Enable Silverlight Debug

 

Technorati Tags: ,