【发布时间】:2010-02-15 15:36:19
【问题描述】:
我在 VS 2010 RC 中使用 EF 4.0。 我有非常简单的 POCO 类结构,几乎没有依赖关系。每个 POCO 类都有一个带有属性 ID 的基本泛型类(EntityObject 或 ValueObect)。我有几个 CRUD 测试,只有一个有效。这个非常简单,对象没有任何依赖关系。但是,当我使用 FK 依赖项测试某些东西时,我总是会遇到相同的错误:System.Data.UpdateException:在多个位置生成跨实体或关联共享的值。检查映射是否不会将 EntityKey 拆分为多个存储生成的列。 我已经用谷歌搜索了,但我发现这个异常的唯一原因是使用了几个上下文,这不是我的情况。
using (IEntityModelContext context = new EFDataContext()) {
var licTypeFact = context.GetFactory<LicenceType>();
var metaValFact = context.GetFactory<MetaValue>();
var cultSpecFact = context.GetFactory<CultureSpecificValue>();
LicenceType licType = licTypeFact.CreateObject();
Assert.IsNotNull(licType);
Assert.IsTrue(licType.IsTransient);
licType.AdvancedFeatureSet = true;
licType.BasicFeatureSet = true;
licType.MaxUsers = 10;
licType.MonthDuration = 1;
MetaValue licTypeName = metaValFact.CreateObject();
licTypeName.Name = "TestLicType";
CultureSpecificValue licNameEng = cultSpecFact.CreateObject();
licNameEng.Value = "Test Licence";
licNameEng.Culture = context.CultureRepository.Load(cult => cult.Name == "Eng");
licNameEng.MetaValue = licTypeName;
licTypeName.CultureSpecificValues = new List<CultureSpecificValue>();
licTypeName.CultureSpecificValues.Add(licNameEng);
licType.Name = licTypeName;
licType.NumberOfQuestionsPerSurvey = 1;
licType.NumberOfResponsesPerSurvey = 2;
licType.NumberOfSurveys = 3;
licType.PerUserPrice = 10;
licType.Price = 100;
context.LicenceTypeRepository.Add(licType);
int res = context.SaveChanges();
那么这个异常的原因是什么?
【问题讨论】:
-
这里没有显示很多自定义代码,我强烈怀疑错误存在。我建议删除间接层并仅使用上下文。错误会消失。然后将间接添加回来,直到错误再次出现。
-
感谢克雷格。我重新生成了 edmx,这个问题就消失了。
标签: .net entity-framework visual-studio-2010