未在实体容器上设置“对象引用”保存更改

时间:2011-02-24 15:11:57

标签: c# entity-framework

当我保存对实体对象所做的更改时,我一直遇到错误。这是失败的代码。

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;
                }
            }
        }
    }
}

堆栈跟踪:

   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(EntitySetBase 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

我已经检查了所有内容,而且我对此很难过。它在实体的容器中注册。更新映射到存储过程,是映射问题吗?

1 个答案:

答案 0 :(得分:2)

看起来您正在从存储过程中读取对象,更新它,然后通过ObjectContext.SaveChanges提交。我能看到这种崩溃的唯一方法是,如果某种程度上,SP正在向你返回一个破坏EF的格式错误的对象。

我建议您评论一下您的SP电话,然后加入类似

的内容
User U = userContainer.Users.FirstOrDefault(u => u.UserName == username);

并查看是否可以解决问题。如果是这样,你会知道问题是你的SP。

此外,你的SP被称为usp_MVC_Users_getbyemailaddress,但是你传递了一个用户名 - 这是正确的,还是你可能正在调用错误的sproc(或者传入错误的参数)。