【发布时间】:2012-06-13 15:31:03
【问题描述】:
[在实体框架 5.0 RC 中使用 Code First DbContext]
ID 派生自其导航属性的实体
public class Domain
{
private string _id;
private SecondLevelDomain _secondLevelDomain;
private TopLevelDomain _topLevelDomain;
public string Id
{
get
{
// Trigger setter synthesis
Id = null;
return _id;
}
set
{
string parentId = String.Empty;
if (Sld.Id != null)
output += Sld.Id + " ";
if (Tld.Id != null)
output += Tld.Id;
_id = parentId;
}
}
public string SecondLevelDomainId
{
get;
set;
}
[ForeignKey("SecondLevelDomainId")]
public SecondLevelDomain Sld
{
get
{
return _secondLevelDomain
?? (_secondLevelDomain = new SecondLevelDomain());
}
set
{
Debug.WriteLine("Foreign Setter Not Called Before Its Too Late");
_secondLevelDomain = value;
}
}
public string TopLevelDomainId
{
get;
set;
}
[ForeignKey("TopLevelDomainId")]
public TopLevelDomain Tld
{
get
{
return _topLevelDomain
?? (_topLevelDomain = new TopLevelDomain());
}
set { _topLevelDomain = value; }
}
}
从数据库创建域时父 ID 评估为空
public CheckDomainInDatabase(string domainId) {
var domainFromDatabase = Repositor.Domains.Find(domainId);
}
InvalidOperationException: The value of a property that is part of an object's key does not match the corresponding property value stored in the ObjectContext. This can occur if properties that are part of the key return inconsistent or incorrect values or if DetectChanges is not called after changes are made to a property that is part of the key.
我需要能够使用聚合标识符检索这些域,因为我需要修改它们的一些其他属性(未显示) - 但这个错误让我偏离了轨道......
【问题讨论】:
标签: c# .net sql entity-framework orm