Many times I have students that think they are never going to make it as a developer. They feel that since they continually have to google errors, look up code and ask questions that they are not a developer. Many people feel that because they cannot bust out an entire application from memory that they are phony and not a real developer.
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.
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.
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.
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.
Counting the number of users in a role in Identity seemed to me to be a very straight forward task. It turned out to be a little more challenging than I had expected but once I figured out the correct linq statement it was rather easy to get working.
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.