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.

Continue reading

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

GitHub: Custom Identity Management

I have blogged previously about Identity and will continue to do so because I think it is a powerful and easy to use package for user and role management. The one thing that it is missing from the base install is a web based method for an administrator to manage those users and roles. There is a nuget package that I have used and talked about before called the IdentitySample. This package very easy to install; however, the problem is that it changes the namespace of over 30 different files. It will also completely wipe out any code on the shared layout as well as the home index file unless you step through and approve or disallow identity to change every file individually.

Continue reading

Working with Identity Users and Roles – Accessing Roles After User Login

There is an issue in the Identity framework when the business requirements need a developer to interact with the roles that a user is assigned to as soon as the user object is created upon successful login. The Identity framework has a bug that has been posted online numerous times but is hard to identify through all of the other code that users post. Luckily there is a very easy fix for it.

Continue reading

Extending Identity – Adding Roles at User Registration

Depending on the application there may be times that it is desirable or required to add a user to a role immediately when they register. Other times you may want to add users to various roles depending on what privileges you want them to have. This is very easily accomplished in the Register method of the AccountController. This is the third post in a 3 part series on extending the ASP.NET Identity Framework.

Continue reading