gpt4 book ai didi

c# - Entity Framework SaveChanges() NullReferenceException, DisplayNameAttribute

转载 作者:行者123 更新时间:2023-11-30 18:46:11 25 4
gpt4 key购买 nike

我被卡住了,正在寻找任何想法。

我有一个实体无法保存更改(所有其他实体都可以)。我已经剥离了我的代码和属性来重现,没有任何额外的东西。我已经检查过我可以保存直接访问 SQL Server 的更改。我通过删除实体并重新添加它来刷新 EF 模型。此精简代码不涉及显示名称。

英孚 6.1

这是失败的测试代码:

    [TestMethod()]
public void SaveEntity_ChangeData_ExpectNoErrors()
{
CaseEntities caseEntities = new CaseEntities();
// Get an existing Entity (this line works)
var entity = caseEntities.RecordsDistributionPreferences.Find("0001");
// Change a field
entity.ModifiedBy = "duncan";

// get row version before save
byte[] rowVersionBefore = entity.RowVersion;

// Save Changes (this next line throws null reference exeption)
caseEntities.SaveChanges();

// get row version after save
byte[] rowVersionAfter = entity.RowVersion;

// Check that row version are different
Assert.AreNotEqual(BitConverter.ToInt64(rowVersionBefore.Reverse().ToArray(), 0), BitConverter.ToInt64(rowVersionAfter.Reverse().ToArray(), 0));
}

异常和 StackTrace:

NullReferenceException {"Object reference not set to an instance of an object."}
at System.ComponentModel.DisplayNameAttribute.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.HashSet`1.InternalGetHashCode(T item)
at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value)
at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection)
at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(Type type)
at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<>c__DisplayClass4.<GetAttributes>b__3(PropertyInfo pi)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo)
at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildPropertyValidator(PropertyInfo clrProperty)
at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildValidatorsForProperties(IEnumerable`1 clrProperties, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties)
at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildTypeValidator[T](Type clrType, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties, Func`3 validatorFactoryFunc)
at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildEntityValidator(InternalEntityEntry entityEntry)
at System.Data.Entity.Internal.Validation.ValidationProvider.GetEntityValidator(InternalEntityEntry entityEntry)
at System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary`2 items)
at System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items)
at System.Data.Entity.DbContext.GetValidationErrors()
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at SCC.Case.Entities.Test.RecordsDistributionPreferenceTest.SaveEntity_ChangeData_ExpectNoErrors() in c:\Dev\SCC\Case\Dev\src\SCC.Case.Entities.Test\RecordsDistributionPreferenceTest.cs:line 40

最佳答案

找到了!

问题出在保存实体有外键的另一个实体上。这个其他实体只是缺少 DisplayName 的本地化资源项。

我发现问题是我剥离了有问题的数据库表的所有外键,然后刷新数据模型(从数据库)。错误消失了。  所以我把每个外键都加回来,刷新,测试,直到我得到错误。然后我查看了外键实体的 DisplayNames,发现少了一个。 

关于c# - Entity Framework SaveChanges() NullReferenceException, DisplayNameAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com