Tuesday, January 27, 2009

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...

Tuesday, January 27, 2009 10:10:34 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, January 24, 2009

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

 | 
Saturday, January 24, 2009 1:15:39 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, January 22, 2009

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: ,
Thursday, January 22, 2009 11:12:02 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Sunday, January 18, 2009

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.

 

Sunday, January 18, 2009 9:20:44 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Tuesday, January 13, 2009

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: ,
Tuesday, January 13, 2009 8:35:02 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, December 18, 2008

So I'm working with Silverlight these days.  It's an excellent technology but it's got a fairly steep learning curve.  For the past several years I've been working predominantly with ASP.Net.  As a matter of fact, I've really gotten pretty good at it!  In addition to all of the in's and out's of ASP.Net, including the page lifecycle, custom controls, etc, I had gotten really comfortable with CSS and I've even stopped saying "I hate JavaScript" and started liking it.  Of course, with JQuery, it is really pretty good to work with.  But I digress.

Anyway, all of that stuff I have learned is out the window now.  Silverlight is completely different.  The easy part is that all of my C# code goes right to the client, as well as running on the server.  So much for JavaScript.  But there is no HTML, no ASP.Net controls.  None of it (at least in the apps I am writing).  This is not an ASP.Net application with a little Silverlight mixed in.  The entire UI for this business application is Silverlight.  The only html/asp.net is the page that hosts it.  So all of the UI is written in XAML.  XAML is cool and very powerful but takes getting used to and when you start with it, you are starting from scratch.  It's not like writing a web app or a windows forms app at all.  But don't get me wrong, I like it.

Anyway, the purpose for this first Silverlight post...

So I'm working along and things are going good.  My app is coming along and working as well as it should in the very rough state it is in.  After making a bunch of changes I fire up the debugger and run my Silverlight application in Internet Explorer.  "Hmmm, that's weird, it shouldn't do that" I think to myself.  I checked the code, and sure enough, it should not do "that".  So I run it again. Same result.  My code is running without the recent changes I made.  Ok, I try a bunch of stuff, not necessarily in this order: Build my Solution, "Rebuild" my solution, "Clean" my solution, put obvious changes in my UI and re run it, restart Internet Explorer, Ctl-F5 to clear IE, try Firefox, close/reopen my solution, close/reopen VS2008, reboot my machine.  I probably tried a few more things but no matter what I did, the application continued to show the UI before my changes.  At some point in all of this, it became clear that my XAP file was not getting updated in my web project.  I tried deleting it, figuring that would signal Visual Studio to replace it when it builds again.  No luck.  I manually placed a current XAP file (from the Silverlight app) into my web project.  Now when I run my application is perfectly up to date!  So I am positive on the problem but unsure how to fix it. I figured that in my Property Pages of my Silverlight Application I'd find something telling me where/when/what to do with the XAP file.  But I couldn't find it.  Next I searched the Property Pages of my Web Application.  Presto, there it is!  From a web application, select the "Silverlight Application" tab.  When I did the list of projects was empty!  I have no idea how it got to be that way.  But I clicked "Add" and the rest was self explanatory.  I picked my Silverlight project and selected where I wanted the XAP file to go. 

 

image

Technorati Tags: , ,
Thursday, December 18, 2008 9:04:51 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback

Last night at the Philly.Net meeting Rob gave a cool talk about Live Mesh.  I had heard of it before but hadn't paid too much attention.  But now that I've gotten a good look at it, I am impressed.  So what can I do with Mesh?  Probably a lot.  But at first two things jump out.  I can synchronize files between multiple devices (my home PC, my laptop, a phone, etc).  Plus in addition to the files being synced, I can go to my Live Desktop and view the files there.  The Live Desktop is just a web site though, so it is accessible from anywhere.  So if I am traveling without my laptop and I want to access one of the files I've got in Mesh, I can get to them from any browser.  Cool!  But from my Live Desktop I can also connect remotely into my home computer (or any computer on my Live network). That way I can not only access files stored on that PC, but I can actually control the computer just as if I was sitting in front of it.  As a matter of fact, I'm doing it right now.  I'm blogging this from my home PC, but I'm sitting in front of my laptop. I'm connected via Live Mesh into my home PC and running Live Writer on my home PC.  Ok, file synchronization and remote desktop are nothing new.  But what makes this cool is just how easy it is. And it all seems to work very quickly too.  Very cool!  Rob also demonstrated some other cool features, like you can write applications to run on Live Mesh, and you can write applications that use Live Mesh.  Maybe I'll get to that one day but for now, I'm having fun with Mesh and it is useful for me in business too.  Awesome.

Thursday, December 18, 2008 8:25:39 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, December 16, 2008

If you are coming out to the Philly.Net meeting on Wednesday, don't forget to bring some cans of food or other non-perishables.  We are collecting food to donate to a local food bank.  Steve Andrews will have the exact details tomorrow night.  All year round many people in our area are hungry.  Let's try to make a difference tomorrow night by collecting a lot of food to help.  Those of us in the Philly.Net leadership have wanted to be more charitable for a while now, it is nice that Steve got this idea going.  Check out GeekFoodDrive for more details on the program.

If you have any other similar ideas and want to get involved, just let us know.

Tuesday, December 16, 2008 7:48:51 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, December 01, 2008

I used to be Andy the Employee, but now I am Andy the Contractor.  Ok, enough election puns.  Actually I would write more but I can't think of any right now.

After many years of working as an employee, I have decided to become and Independent Contractor/Consultant.  What does that mean?  Well really it isn't all that different.  From day to day, my job will still be the same but now I have to make sure I can find enough work to keep myself busy.  So I guess my job will be harder.  Plus I get no health benefits and I have to pay more taxes out of my paycheck.  Hey, this isn't sounding too great!  Actually, a lot of software developers work this way and they seem to like it.  There are pros to this work model too.  You typically get paid more to cover the extra taxes, etc.  I hope that turns out to be the case.  And you get to choose the kind of projects you want to take on which is great.  I have worked for several consulting companies over the years and I didn't always like the assignments I got, well now I am in control!

Now I have a lot of work to do and I hope to blog about the process.  I need to set up a company (incorporate), figure out how to track expenses and billing, and finish a list of other business management stuff.  I've been talking to a lot of people about the benefits of different business structures:  DBA (Doing Business As), LLC (Limited Liability Company), and S-Corp.  It seems that they each have different tax benefits in the order I listed them.  S-Corp has, from what I can tell, the best tax breaks but seems to be the hardest to manage.  I think most software developer types choose LLC because of the benefits/complications ration is pretty good.  I need to figure it all out.

Also, I plan on learning some new technology and hopefully some good blog posts will come of it.  In the meantime, here goes nothing...

Monday, December 01, 2008 10:20:02 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [2]  |  Trackback