Tuesday, March 29, 2011

Entity Framework 4.1 Entry Extensions

I have been working with the Microsoft Entity Framework version 4.1 aka "Code First" for about the last 3 weeks and have been really enjoying the ease and flexibility of setting things up and getting my CRUD operation working so smoothly. However, I had the need to create modify some entities in my database and was starting down the path of passing in the new entity, then querying the context for the old entity and finally setting the values of the old entity to those of the new entity. This worked for a little while, when I only had simple entities. However, my entities started getting more complex and I found myself adding more properties and always forgetting to update the modify operation.

So I started doing some research and found that I could use the Attach method on the DbSet to add the modified entity. However, this left the entity in the UnChanged state and that did not really do me any good. So with a little more research I found this Blog Post by Julie Lerman about Round Tripping a Timestamp Field in EF 4.1 with MVC 3 that discusses the DbContext.Entry method and how I can set the State as I attach the entity.

I had also in my secondary research found this post about creating extensions for the ObjectContext in the regular Entity Framework and this led to the creation of my own Extensions for the DbContext object as seen below...

I now use these extension methods as shown below.

Monday, March 21, 2011

Good Drupal Books

Had the following Drupal books recommended today...

I have read Pro Drupal Development and it was an excellent book that helped me get up to speed quickly on developing modules for Drupal. I would highly recommend this book. I have not read the others, but plan on checking them out soon.

Drupal, MAMP & Drush

I am attending a Lullabot.com Drupal API &Module Development course at work this week. Learning a lot of great thoings so far. I worked on a Drupal project last year and created a custom module, but what I have learned so far in one day, could have probably saved me a close to a week as I was trying to figure it out on my own.

At the end of class today, the instructors were going over some tips for Drupal developers and showed off the awesome Drush, Drupal Shell. It is amazing what this tool can do, and being able to have the power of the command line to very nice. However, since I am running Drupal on MAMP, I was getting an error about not having the correct amount of RAM configured. However, I found this "getting drush to work on MAMP setup" article that had the key that worked for me. Create a .bash_profile file and adding an alias:

alias drush='/Applications/MAMP/bin/php5/bin/php /usr/bin/drush/drush.php'
export COLUMNS

Friday, March 18, 2011

ASP.NET MVC & MvcBuildViews set to true causes Build Error

I recently set the property in my ASP.NET MVC web application for the Release mode as recommended by K. Scott Allen in Notes on Building Razor Views. This was working fine for a few days. Then yesterday, I started getting this error every time I would try a Release build.

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

I went back to the comments from the blog post and found this one, where someone else was having the same issue and they solved the issue by deleting the obj\ folder, as recommended by the StackOverflow MVC BuildViews not working correctly question. I tried this and unfortunately it did not resolve my issue. Another answer further down suggested changing the PhysicalPath value for the AspNetCompiler value in the BuildViews task. I tried the recommended path:

That also did not work for me, so I followed a link to the use of Web Deployment Projects with ASP.NET MVC and noticed that they were using the following PhysicalPath setting.

I changed my setting to this value, $(ProjectDir)\..\Web created this directory and now things are working properly again.

Monday, March 7, 2011

Flush DNS on a Mac

So there were some network changes over the weekend at work and I needed to flush my DNS entries, so I did a quick Google search and found this great reference for flushing DNS on almost any system.

Specifically for  Mac OSX Leopard you issue the following command:

dscacheutil -flushcache