Visual Studio Custom Project Templates

After developing a number of web sites we tend to find a design and structure that we default to. We build out the same architecture, use the same design patterns and include many of the same resources. This can all take time to build out each project. Visual Studio provides us a way to shorten this process. It allows us to create custom solution templates that contain the structure and resources that we add to all projects.

Continue reading

Advertisements

Custom Scaffolding

Scaffolding is a great time saver. However, as developers progress they find different patterns and techniques that they consistently use in their projects and will find themselves continually changing scaffolding the same way over and over. This can become very tedious and time consuming. Fortunately Microsoft has allowed for developers to customize scaffolding for each project. They have set up the scaffolding functionality to check for scaffolding templates in the Visual Studio project and use those templates over the global templates if any are found. To take advantage of this all you have to do is make a copy of the templates you want, modify them and include them in your project.

Continue reading

Editing Records and Updating Images with Entity Framework

Editing records using EF is very simple. Visual Studio even scaffolds out all of the code for you if you are simply updating a textual record. However, if you are also trying to update an image or file things get a little tricky. Trying to determine if a new file has been upload and then either delete the old image and upload the new one or repopulate the old image name so that it is not removed when the new information is updated has proven to be quite a challenge. This is because of the way the EF tracks objects. It seems that we could simply use the Find() method to look up the old image however that causes the following error:

Attaching an entity of type ‘X’ failed because another entity of the same type already has the same primary key value. This can happen when using the ‘Attach’ method or setting the state of an entity to ‘Unchanged’ or ‘Modified’ if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the ‘Add’ method or the ‘Added’ entity state to track the graph and then set the state of non-new entities to ‘Unchanged’ or ‘Modified’ as appropriate.

To get around this problem we have to use a special AsNoTrackting() method to get the current photo or file name without creating a second entity for EF to track which is what the Find() method does. The following method is from a Pet class which uses “context” as the DbContext. We use this context to query the database for the current photo/file name before we update the record.

public Pet UntrackedFind(int id)
{
return context.Set().AsNoTracking().Where(p => p.PetID == id).FirstOrDefault();
}

Next we simply call this method in from the HttpPost version of the Edit ActionResult to update the record and either delete the old photo/file and add the new one or to populate the photo/file property with the exiting file name so that it is not set to an empty string when the rest of the entity is updated.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PetID,OwnerID,Name,PetPhoto,SpecialNotes,IsActive,DateAdded")] Pet pet, HttpPostedFileBase petPhoto)
{

if (ModelState.IsValid)
{

string oldFileName = db.Pet.UntrackedFind(pet.PetID).PetPhoto;

if (petPhoto != null)
{

var savePath = Server.MapPath("~/Content/Images/");
ImageUtilities.Delete(savePath, oldFileName);

var imageName = petPhoto.FileName;

// Begin code to upload file or image
// The following is a call to a custom ImageUtilities class which resizes
// the image, creates a thumbnail and uploads both to the server.
ImageUtilities.ResizeImage(savePath, imageName, Image.FromStream(petPhoto.InputStream), 500, 75);
// End code to upload file or image

pet.PetPhoto = imageName;
}
else
{
pet.PetPhoto = oldFileName;
}

db.Pet.Update(pet);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(pet);
}

Adding and Referencing Additional Data in the Asp.net Identity AspNetUsers Table

I have talked about Identity and how useful it is. There are somethings that I wish were included that are not however. This is when the ability to extend or modify Identity comes in useful. Storing a user’s name or other personal information in the AspNetUsers table is one of those things. In this post I am going to show how to add a FirstName and LastName column to the AspNetUsers table and then pull that information out when a user signs in.

Continue reading

Creating a YouTube application with ASP.NET MVC and the YouTube API

YouTube has become the main video hosting site on the internet. Not only can individuals and companies post videos but they can find numerous videos posted by other users. These videos may be very useful in helping to teach kids, employees or others specials lessons or skills. The problem is making it easy for users to view these videos without all of the other suggested videos and garbage on the YouTube site itself. Creating an application to manage the videos that you want users to see without all the other stuff is pretty straight forward.

Continue reading