"Object Reference" not set on entity container save changes
I keep encountering an error when I go save changes I have made on my entity object. Here is the code that fails.
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
//We dont wont to do anything if there is no username supplied
if (!string.IsNullOrEmpty(username))
{
User user = null;
using (UserContainer userContainer = new UserContainer())
{
//Gets the user from Enity Stored Procedure
List<User> results = userContainer.usp_MVC_Users_getbyemailaddress(username).ToList();
//To prevent an exeption we test how many records are returned, then assign it
if (results.Count() > 0)
user = results.First();
if (user != null)
{
string encryptedOldPassword = encrypt(oldPassword);
//does the supplied password match the old password?
if (user.Password == encryptedOldPassword)
{
//Set new password
user.Password = encrypt(newPassword);
//Save the info
// userContainer.Refresh(System.Data.Objects.RefreshMode.ClientWins, user);
userContainer.SaveChanges();
return true;
}
}
}
}
}
stack trace:
at System.Data.Mapping.ViewGeneration.ViewgenGatekeeper.EnsureAllCSpaceContainerSetsAreMapped(IEnumerable`1 cells, ConfigViewGenerator config, StorageEntityContainerMapping containerMapping)
at System.Data.Mapping.ViewGeneration.ViewgenGatekeeper.GenerateViewsFromCells(List`1 cells, ConfigViewGenerator config, CqlIdentifiers identifiers, StorageEntityContainerMapping containerMapping)
at System.Data.Mapping.ViewGeneration.ViewgenGatekeeper.GenerateViewsFromMapping(StorageEntityContainerMapping containerMapping, ConfigViewGenerator config)
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGenerateViews(EntityContainer container, Dictionary`2 resultDictionary)
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGetGeneratedViews(EntityContainer container)
at System.Data.Common.Utils.Memoizer`2.c__DisplayClass2.b__0()
at System.Data.Common.Utils.Memoizer`2.Result.GetValue()
at System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg)
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace, StorageMappingItemCollection storageMappingItemCollection)
at System.Data.Mapping.StorageMappingItemCollection.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace)
at System.Data.Mapping.Update.Internal.ViewLoader.InitializeEntitySet(EntitySetBase entitySetBase, MetadataWorkspace workspace)
at System.Data.Mapping.Update.Internal.ViewLoader.SyncInitializeEntitySet[TArg,TResult](EntitySetBase entitySetBase, MetadataWorkspace workspace, Func`2 evaluate, TArg arg)
at System.Data.Mapping.Update.Internal.ViewLoader.SyncContains[T_Element](EntitySetBase entitySetBase, MetadataWorkspace workspace, Set`1 set, T_Element element)
at System.Data.Mapping.Update.Internal.ViewLoader.IsServerGen(EntitySetBase entitySetBase, MetadataWorkspace workspace, EdmMember member)
at System.Data.Mapping.Update.Internal.ExtractorMetadata..ctor(E开发者_运维知识库ntitySetBase entitySetBase, StructuralType type, UpdateTranslator translator)
at System.Data.Mapping.Update.Internal.UpdateTranslator.GetExtractorMetadata(EntitySetBase entitySetBase, StructuralType type)
at System.Data.Mapping.Update.Internal.ExtractorMetadata.ExtractResultFromRecord(IEntityStateEntry stateEntry, Boolean isModified, IExtendedDataRecord record, Boolean useCurrentValues, UpdateTranslator translator, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
at System.Data.Mapping.Update.Internal.RecordConverter.ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, Boolean useCurrentValues, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
at System.Data.Mapping.Update.Internal.RecordConverter.ConvertOriginalValuesToPropagatorResult(IEntityStateEntry stateEntry, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
at System.Data.Mapping.Update.Internal.ExtractedStateEntry..ctor(UpdateTranslator translator, IEntityStateEntry stateEntry)
at System.Data.Mapping.Update.Internal.UpdateTranslator.LoadStateEntry(IEntityStateEntry stateEntry)
at System.Data.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at CMSMVC.Models.Providers.CmsMembershipProvider.ChangePassword(String username, String oldPassword, String newPassword) in C:\Projects\CMSMVC\CMSMVC\Models\Providers\CmsMembershipProvider.cs:line 129
at CMSMVC.Tests.CmsMembershipProviderTest.ChangePasswordTest() in C:\Projects\CMSMVC\CMSMVC.Tests\CmsMembershipProviderTest.cs:line 105
I've checked everything and I'm pretty stumped on this one. It is registered in the entity's container. Update is mapped to a stored procedure, is it a mapping issue?
It looks like you're reading your object in from a stored procedure, updating it, then committing via ObjectContext.SaveChanges
. The only way I could see this crashing is if somehow, someway the SP is returning to you a malformed object that's breaking EF.
I would recommend commenting out your SP call, and putting in something like
User U = userContainer.Users.FirstOrDefault(u => u.UserName == username);
and see if that fixes things. If so, you'll know the problem is your SP.
Also, your SP is called usp_MVC_Users_getbyemailaddress
, but you're passing in a username to it - is this correct, or are you maybe calling the wrong sproc (or maybe passing in the wrong parameter).
精彩评论