考虑这种情况:
public class House
{
public string Name { get; set; }
public virtual Person Person1 { get; set; }
public virtual Person Person2 { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public House House { get; set; }
}
// Relationships
this.HasOptional(t => t.Person1)
.WithRequired(t => t.House);
this.HasOptional(t => t.Person2)
.WithRequired(t => t.House);
当我尝试插入 House 的实例时,会抛出 MetaDataException:
指定的架构无效。错误:(36,6):错误 0040:类型 House_Person1 未在命名空间 Company.Models 中定义(别名=Self)。
经过更多的黑客攻击,发现这可行:
// Relationships
this.HasOptional(t => t.Person1)
.WithRequired();
this.HasOptional(t => t.Person2)
.WithRequired();
但是我不会让 EF 填充 Person.House 属性。我可以模仿这种行为(如下所示)吗?
public virtual Person Person1
{
get
{
return _person1;
}
set
{
_person1 = value;
_person1.Haushalt = this;
}
}
public virtual Person Person2
{
get
{
return _person2;
}
set
{
_person2 = value;
_person2.Haushalt = this;
}
}